Updated: Comment #N

Problem/Motivation

The code is still based on the old assumption that fields can be re-used for other entity types and relies only on the entity keys and not the base fields.

Proposed resolution

1. Only check the entity type the field is attached to.
2. use \Drupal::entityManager()->getFieldDefinitions($entity_type) of the entity manager to check for a conflict with all base fields.

Remaining tasks

User interface changes

API changes

Comments

swentel’s picture

Status: Active » Needs review
StatusFileSize
new5.84 KB

Here we go. I've added a test to the Field UI tests because this also fixes an error in FieldOverview which would blow up very easily when trying to show the message with the error. Also cleaned up a leftover todo in FieldTypePluginManager along the way.

swentel’s picture

Ok, this needs a reroll now the field class rename patch is in of course.

swentel’s picture

StatusFileSize
new5.87 KB

Reroll

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
    @@ -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 {
    

    Best forgotten @todo ever :)

  2. +++ b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php
    @@ -306,13 +306,11 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll
    +    // Disallow reserved field names.
    +    $disallowed_field_names = array_keys($entity_manager->getDefinition($this->entity_type)->getKeys());
    +    $disallowed_field_names += array_keys($entity_manager->getFieldDefinitions($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)));
         }
    

    Everything on a content entities is a field, so there can't be a key that is not also a field, so the first line shouldn't be necessary.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new5.76 KB
new1000 bytes

Right, duh, where's my mind.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Now it looks great :)

berdir’s picture

5: 2199811-5.patch queued for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php
@@ -306,13 +306,10 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll
+    $disallowed_field_names = array_keys($entity_manager->getFieldDefinitions($this->entity_type));

We can use getBaseFieldDefinitions() here.

The last submitted patch, 5: 2199811-5.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new5.76 KB
new897 bytes
berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7099e22 and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.