diff --git a/mod/data/classes/search/entry.php b/mod/data/classes/search/entry.php
index b1a6fd9fd61..b476cf8d45c 100644
--- a/mod/data/classes/search/entry.php
+++ b/mod/data/classes/search/entry.php
@@ -295,6 +295,10 @@ class entry extends \core_search\base_mod {
foreach ($filteredcontents as $content) {
$classname = $this->get_field_class_name($content->fieldtype);
+ if (!$classname) {
+ $content->addtemplateposition = -1;
+ continue;
+ }
$content->priority = $classname::get_priority();
$content->addtemplateposition = strpos($template, '[['.$content->fldname.']]');
}
@@ -342,16 +346,22 @@ class entry extends \core_search\base_mod {
}
/**
- * Returns the class name for that field type and includes it.
+ * Returns the class name for the given field type and includes it.
*
* @param string $fieldtype
- * @return string
+ * @return string|null It will return the class name or null if the field type is not available.
*/
protected function get_field_class_name($fieldtype) {
global $CFG;
$fieldtype = trim($fieldtype);
- require_once($CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php');
+
+ $fieldpath = $CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php';
+ if (!file_exists($fieldpath)) {
+ return null;
+ }
+
+ require_once($fieldpath);
return 'data_field_' . $fieldtype;
}
diff --git a/mod/data/edit.php b/mod/data/edit.php
index 809a8435d95..26b3a697c73 100644
--- a/mod/data/edit.php
+++ b/mod/data/edit.php
@@ -258,25 +258,33 @@ if ($data->addtemplate){
$replacements = array();
///then we generate strings to replace
- foreach ($possiblefields as $eachfield){
+ foreach ($possiblefields as $eachfield) {
$field = data_get_field($eachfield, $data);
-
// To skip unnecessary calls to display_add_field().
- if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
- // Replace the field tag.
- $patterns[] = "[[".$field->field->name."]]";
+ if (strpos($data->addtemplate, "[[" . $field->field->name . "]]") !== false) {
+ // Display an error in case the field type is not found.
$errors = '';
if (!empty($fieldnotifications[$field->field->name])) {
foreach ($fieldnotifications[$field->field->name] as $notification) {
$errors .= $OUTPUT->notification($notification);
}
}
- $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
- }
-
- // Replace the field id tag.
- $patterns[] = "[[".$field->field->name."#id]]";
- $replacements[] = 'field_'.$field->field->id;
+ // Replace the field tag.
+ $fielddisplay = '';
+ if ($field->type === 'unknown') {
+ if (has_capability('mod/data:manageentries', $context)) {
+ // Display notification for users that can manage entries.
+ $errors .= $OUTPUT->notification(get_string('missingfieldtype', 'data',
+ (object)['name' => $field->field->name]));
+ }
+ } else {
+ $fielddisplay = $field->display_add_field($rid, $datarecord);
+ }
+ $patterns[] = "[[" . $field->field->name . "]]";
+ $replacements[] = $errors . $fielddisplay;
+ } // Replace the field id tag.
+ $patterns[] = "[[" . $field->field->name . "#id]]";
+ $replacements[] = 'field_' . $field->field->id;
}
if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
diff --git a/mod/data/field.php b/mod/data/field.php
index eb173872ad1..efc4e2d5280 100644
--- a/mod/data/field.php
+++ b/mod/data/field.php
@@ -268,9 +268,15 @@ switch ($mode) {
// Print confirmation message.
$field = data_get_field_from_id($fid, $data);
- echo $OUTPUT->confirm(''.$field->name().': '.$field->field->name.'
'. get_string('confirmdeletefield','data'),
- 'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
- 'field.php?d='.$data->id);
+ if ($field->type === 'unknown') {
+ $fieldtypename = get_string('unknown', 'data');
+ } else {
+ $fieldtypename = $field->name();
+ }
+ echo $OUTPUT->confirm(''.$fieldtypename.': '.$field->field->name.'
'.
+ get_string('confirmdeletefield', 'data'),
+ 'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
+ 'field.php?d='.$data->id);
echo $OUTPUT->footer();
exit;
@@ -336,6 +342,9 @@ $plugins = core_component::get_plugin_list('datafield');
$menufield = array();
foreach ($plugins as $plugin=>$fulldir){
+ if (!is_dir($fulldir)) {
+ continue;
+ }
$menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
}
asort($menufield); //sort in alphabetical order
@@ -379,6 +388,7 @@ if (($mode == 'new') && (!empty($newtype))) { // Adding a new field.
$table->wrap = array(false,false,false,false);
if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
+ $missingfieldtypes = [];
foreach ($fff as $ff) {
$field = data_get_field($ff, $data);
@@ -397,15 +407,30 @@ if (($mode == 'new') && (!empty($newtype))) { // Adding a new field.
'mode' => 'delete',
));
- $table->data[] = array(
- html_writer::link($displayurl, $field->field->name),
- $field->image() . ' ' . $field->name(),
+ // It display a notification when the field type does not exist.
+ $deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
+ $editlink = html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
+ if ($field->type === 'unknown') {
+ $missingfieldtypes[] = $field->field->name;
+ $fieldnamedata = $field->field->name;
+ $fieltypedata = $field->field->type;
+ $fieldlinkdata = $deletelink;
+ } else {
+ $fieldnamedata = html_writer::link($displayurl, $field->field->name);
+ $fieltypedata = $field->image() . ' ' . $field->name();
+ $fieldlinkdata = $editlink . ' ' . $deletelink;
+ }
+
+ $table->data[] = [
+ $fieldnamedata,
+ $fieltypedata,
$field->field->required ? get_string('yes') : get_string('no'),
shorten_text($field->field->description, 30),
- html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
- ' ' .
- html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
- );
+ $fieldlinkdata
+ ];
+ }
+ if (!empty($missingfieldtypes)) {
+ echo $OUTPUT->notification(get_string('missingfieldtypes', 'data') . html_writer::alist($missingfieldtypes));
}
}
echo html_writer::table($table);
diff --git a/mod/data/lang/en/data.php b/mod/data/lang/en/data.php
index d5074ca9c87..8098ab7f2a7 100644
--- a/mod/data/lang/en/data.php
+++ b/mod/data/lang/en/data.php
@@ -231,6 +231,11 @@ $string['invalidfieldid'] = 'Field ID is incorrect';
$string['invalidfieldname'] = 'Please choose another name for this field';
$string['invalidfieldtype'] = 'Field type is incorrect';
$string['invalidid'] = 'Incorrect data ID';
+$string['missingfieldtype'] = 'Field type for {$a->name} not found';
+$string['missingfieldtypes'] = 'The following fields do not have their corresponding field types installed and will not be included in the forms when adding or editing entries.
+ Their labels may still show on the form, so please update the "Add entry template" accordingly:';
+$string['missingfieldtypeimport'] = 'The following fields were not imported because their corresponding field types are not installed:';
+$string['unknown'] = 'Unknown field';
$string['invalidpreset'] = '{$a} is not a preset.';
$string['invalidrecord'] = 'Incorrect record';
$string['invalidurl'] = 'The URL you just entered is not valid';
diff --git a/mod/data/lib.php b/mod/data/lib.php
index cf98e01e6d3..877310c31a7 100644
--- a/mod/data/lib.php
+++ b/mod/data/lib.php
@@ -334,6 +334,10 @@ class data_field_base { // Base class for Database Field Types (see field/*/
if (empty($this->field)) { // No field has been defined yet, try and make one
$this->define_default_field();
}
+ // Throw an exception if field type doen't exist. Anyway user should never access to edit a field with an unknown fieldtype.
+ if ($this->type === 'unknown') {
+ throw new \moodle_exception(get_string('missingfieldtype', 'data', (object)['name' => $this->field->name]));
+ }
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
echo '