Problem/Motivation

EntityReferenceRevisionsItem extends from FieldItemBase and includes almost the same code as EntityReferenceItem (Which also extends from FieldItemBase), So, instead
of extending FieldItemBase and repeating the same code, extend from EntityReferenceItem and clean up the redundant code.

Proposed resolution

Remaining tasks

User interface changes

API changes

Comments

Anushka-mp’s picture

Status: Active » Needs review
StatusFileSize
new7.87 KB
miro_dietiker’s picture

Oh wow nice reduction!
I cannot RTBC this as i'm not deep enough into entity reference topic to know what remaining stuff is needed... And if we have enough test coverage.

berdir’s picture

+++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
@@ -264,43 +179,9 @@ class EntityReferenceRevisionsItem extends FieldItemBase {
-  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
-    $manager = \Drupal::service('plugin.manager.entity_reference_selection');
-    if ($referenceable = $manager->getSelectionHandler($field_definition)->getReferenceableEntities()) {
-      $group = array_rand($referenceable);
-      $values['target_id'] = array_rand($referenceable[$group]);

We still want to keep this method I think, and add a revision id. Not sure how that would actually look like, though.

berdir’s picture

Also, is there a reason you separated this from #2455551: Merge EntityReferenceRevisionsItem & ConfigurableEntityReferenceRevisionsItem? I think it could go together, as further methods are merged together and so on.

I guess there is a risk that something breaks by this, setValue() and similar methods are very complex, but things might be broken right now as well, and it should be easier to keep up with core.

Anushka-mp’s picture

@Berdir, I removed this because it was identical to the parent method. Not sure about revision id there.

berdir’s picture

True, the current implementation is identical. Sounds like it could be a separate issue to generate better values.

jeroen.b’s picture

I remember having some issues with using the default implementation for some functions which caused the paragraphs entity to be saved twice (end result was the same, but I rather prevent extra saves).

So that's something that should be tested.

Berdir queued 1: extend_from-2456281-1.patch for re-testing.

berdir’s picture

Status: Needs review » Needs work

I still think we should do this together with #2455551: Merge EntityReferenceRevisionsItem & ConfigurableEntityReferenceRevisionsItem.

Make sure this works with paragraph by running the paragraph tests with the patch applied.

sasanikolic’s picture

Status: Needs work » Needs review
StatusFileSize
new27.74 KB
new20.87 KB

Merged EntityReferenceRevisionsItem and ConfigurableEntityReferenceRevisionsItem and checked paragraphs tests - they pass.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/entity_reference_revisions.module
    @@ -44,7 +44,7 @@ function entity_reference_revisions_help($route_name, RouteMatchInterface $route
     function entity_reference_revisions_field_info_alter(&$info) {
       // Make the entity reference field configurable.
       $info['entity_reference_revisions']['no_ui'] = FALSE;
    -  $info['entity_reference_revisions']['class'] = '\Drupal\entity_reference_revisions\ConfigurableEntityReferenceRevisionsItem';
    +  $info['entity_reference_revisions']['class'] = '\Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem';
       $info['entity_reference_revisions']['list_class'] = '\Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList';
       $info['entity_reference_revisions']['default_formatter'] = 'entity_reference_revisions_entity_view';
       $info['entity_reference_revisions']['default_widget'] = 'entity_reference_revisions_autocomplete';
    

    The whole hook should be removed and the remaining keys that are not yet set should be merged into the annotation of the remaining class.

  2. +++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
    @@ -36,24 +42,17 @@ use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
    +class EntityReferenceRevisionsItem extends EntityReferenceItem implements OptionsProviderInterface, PreconfiguredFieldUiOptionsInterface {
    

    Can we directly extend ConfigurableEntityReferenceItem?

sasanikolic’s picture

Status: Needs work » Needs review
StatusFileSize
new28.47 KB
new2.98 KB

Uhm, what about that error msg about the new line?
There is an empty line at the end of the file...

berdir’s picture

+++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
@@ -38,11 +38,15 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
+ *   provider = "entity_reference_revisions",

provider is set automatically if it's your module, you only need to specify it if you are providing a plugin for another module (or changing it)

sasanikolic’s picture

StatusFileSize
new28.42 KB
new707 bytes

Ok, deleted the provider from the annotation.

sasanikolic’s picture

Removed some extra/same classes.

LKS90’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
    @@ -7,15 +7,21 @@
    +use Drupal\Component\Utility\SafeMarkup;
    ...
    +use Drupal\Core\Form\OptGroup;
    +use Drupal\Core\Session\AccountInterface;
    ...
    +use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
    

    Unused.

  2. +++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
    @@ -241,7 +217,9 @@ class EntityReferenceRevisionsItem extends FieldItemBase {
    +    if ($notify && isset($this->parent)) {
    +      $this->parent->onChange($this->name);
    +    }
    

    Can you make those just like the occurrence a few lines above that?

          // Notify the parent if necessary.
          if ($notify && $this->parent) {
            $this->parent->onChange($this->getName());
          }
    

    It's mostly about $this->getName() instead of $this->name, but then I wonder why we don't use $this->getParent() either.

  3. +++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
    @@ -323,4 +267,11 @@ class EntityReferenceRevisionsItem extends FieldItemBase {
    +   * {@inheritdoc}
    +   */
    +  public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
    +    return FALSE;
    +  }
     }
     
    

    Missing empty line.

sasanikolic’s picture

Status: Needs work » Needs review
StatusFileSize
new22.46 KB
new1.74 KB

Fixes for the comment above.

LKS90’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/entity_reference_revisions.module
@@ -230,4 +217,4 @@ function entity_reference_revisions_form_field_ui_field_storage_add_form_alter(a
-}
\ No newline at end of file
+}

Pls fix!

Just kidding... The \ No newline at end of file is the old version, if you'd make the mistake, it'd be there twice (see this example:)

@@ -230,4 +217,4 @@ function entity_reference_revisions_form_field_ui_field_storage_add_form_alter(a
   // "Other".
   unset($form['add']['new_storage_type']['#options'][t('Reference revisions')]['entity_reference_revisions']);
   $form['add']['new_storage_type']['#options'][t('Reference revisions')]['entity_reference_revisions'] = t('Other…');
-}
\ No newline at end of file
+ }
\ No newline at end of file

I'll set this to RTBC since I think this is good to go, tested mostly with paragraphs.

miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed

Committed. I guess paragraphs need fixing now. :-)

miro_dietiker’s picture

Looks fine. Paragraph still works perfectly for me.

Status: Fixed » Closed (fixed)

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

Maouna’s picture

I am trying to install the paragraphs module, but I got this:

Fatal error: Access to undeclared static property: Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem::$NEW_ENTITY_MARKER in xxx/htdocs/modules/contrib/entity_reference_revisions/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php on line 204

I think it is related to this issue because I found this in the applied patch:

-class EntityReferenceRevisionsItem extends FieldItemBase {
-
-
-  /**
-   * Marker value to identify a newly created entity.
-   *
-   * @var int
-   */
-  protected static $NEW_ENTITY_MARKER = -1;
+class EntityReferenceRevisionsItem extends ConfigurableEntityReferenceItem implements OptionsProviderInterface, PreconfiguredFieldUiOptionsInterface {

What do you recommend me to do now?

giancarlosotelo’s picture

#25 There is a fix for that #2576767: Fix failing tests.