Commit 7099e22f authored by alexpott's avatar alexpott

Issue #2199811 by swentel: Reserved field names check in Field::preSaveNew() can be simplified.

parent 8f077919
......@@ -14,8 +14,6 @@
/**
* Plugin manager for 'field type' plugins.
*
* @todo Add FieldTypePluginManagerInterface in https://drupal.org/node/2175415.
*/
class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePluginManagerInterface {
......
......@@ -315,13 +315,10 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll
));
}
// Disallow reserved field names. This can't prevent all field name
// collisions with existing entity properties, but some is better than
// none.
foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
if (in_array($this->name, $entity_type->getKeys())) {
throw new FieldException(format_string('Attempt to create field %name which is reserved by entity type %type.', array('%name' => $this->name, '%type' => $entity_type_id)));
}
// Disallow reserved field names.
$disallowed_field_names = array_keys($entity_manager->getBaseFieldDefinitions($this->entity_type));
if (in_array($this->name, $disallowed_field_names)) {
throw new FieldException(format_string('Attempt to create field %name which is reserved by entity type %type.', array('%name' => $this->name, '%type' => $this->entity_type)));
}
// Check that the field type is known.
......
......@@ -385,6 +385,7 @@ protected function validateAddExisting(array $form, array &$form_state) {
* Overrides \Drupal\field_ui\OverviewBase::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
$error = FALSE;
$form_values = $form_state['values']['fields'];
$destinations = array();
......@@ -438,7 +439,8 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['fields_added']['_add_new_field'] = $values['field_name'];
}
catch (\Exception $e) {
drupal_set_message($this->t('There was a problem creating field %label: !message', array('%label' => $instance->getLabel(), '!message' => $e->getMessage())), 'error');
$error = TRUE;
drupal_set_message($this->t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
}
}
......@@ -487,7 +489,8 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
}
catch (\Exception $e) {
drupal_set_message($this->t('There was a problem creating field instance %label: @message.', array('%label' => $instance->getLabel(), '@message' => $e->getMessage())), 'error');
$error = TRUE;
drupal_set_message($this->t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error');
}
}
}
......@@ -497,7 +500,7 @@ public function submitForm(array &$form, array &$form_state) {
$destinations[] = $destination['destination'];
$form_state['redirect_route'] = FieldUI::getNextDestination($destinations, $form_state);
}
else {
elseif (!$error) {
drupal_set_message($this->t('Your settings have been saved.'));
}
}
......
......@@ -383,6 +383,32 @@ function testDeleteField() {
$this->assertNull(field_info_field('node', $this->field_name), 'Field was deleted.');
}
/**
* Tests that Field UI respects disallowed field names.
*/
function testDisallowedFieldNames() {
// Reset the field prefix so we can test properly.
\Drupal::config('field_ui.settings')->set('field_prefix', '')->save();
$label = 'Disallowed field';
$edit = array(
'fields[_add_new_field][label]' => $label,
'fields[_add_new_field][type]' => 'test_field',
);
// Try with an entity key.
$edit['fields[_add_new_field][field_name]'] = 'title';
$bundle_path = 'admin/structure/types/manage/' . $this->type;
$this->drupalPostForm("$bundle_path/fields", $edit, t('Save'));
$this->assertText(t('There was a problem creating field Disallowed field: Attempt to create field title which is reserved by entity type node.', array('%label' => $label)), 'Field was not saved.');
// Try with a base field.
$edit['fields[_add_new_field][field_name]'] = 'sticky';
$bundle_path = 'admin/structure/types/manage/' . $this->type;
$this->drupalPostForm("$bundle_path/fields", $edit, t('Save'));
$this->assertText(t('There was a problem creating field Disallowed field: Attempt to create field sticky which is reserved by entity type node.', array('%label' => $label)), 'Field was not saved.');
}
/**
* Tests that Field UI respects locked fields.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment