Problem/Motivation

The list of view_modes is empty in dynamic_entity_reference_entity_view formatter.

Proposed resolution

Allow users to choose per entity type formatter.

Remaining tasks

None.

User interface changes

Users will have the ability to choose the per entity type formatter in dynamic_entity_reference_entity_view formatter settings.

API changes

API addition only

Data model changes

None

Original report by @SylvainM

The formatter can not found available view_modes.
Patch will follow

CommentFileSizeAuthor
#47 screenshot-d8.dev 2016-11-06 21-14-30.png18.76 KBjibran
#47 screenshot-d8.dev 2016-11-06 21-15-03.png15.11 KBjibran
#47 interdiff.txt2.64 KBjibran
#44 list_of_view_modes_is-2729463-44.patch14.25 KBjibran
#44 interdiff.txt1.01 KBjibran
#41 list_of_view_modes_is-2729463-41.patch14.17 KBCTaPByK
#41 interdiff.txt2.1 KBCTaPByK
#36 list_of_view_modes_is-2729463-36.patch14 KBCTaPByK
#36 interdiff.txt878 bytesCTaPByK
#32 list_of_view_modes_is-2729463-32.patch14.16 KBjibran
#32 interdiff.txt1.28 KBjibran
#30 interdiff.txt3.28 KBCTaPByK
#30 list_of_view_modes_is-2729463-30.patch14.08 KBCTaPByK
#27 list_of_view_modes_is-2729463-27.patch13.45 KBjibran
#27 interdiff.txt2.35 KBjibran
#26 list_of_view_modes_is-2729463-26.patch12.97 KBCTaPByK
#26 interdiff.txt5.61 KBCTaPByK
#23 list_of_view_modes_is-2729463-23.patch12.96 KBjibran
#23 interdiff.txt4.7 KBjibran
#2 dynamic_entity_reference-fix_settings_view_modes_list-2729463-1.patch1.77 KBsylvainm
#7 test-only.patch3.15 KBCTaPByK
#7 dynamic_entity_reference-fix_settings_view_modes_list-2729463-7.patch4.92 KBCTaPByK
#9 interdiff.txt956 bytesCTaPByK
#9 test-only.patch2.78 KBCTaPByK
#9 dynamic_entity_reference-fix_settings_view_modes_list-2729463-9.patch4.56 KBCTaPByK
#13 interdiff.txt4.05 KBCTaPByK
#13 dynamic_entity_reference-fix_settings_view_modes_list-2729463-13.patch4.9 KBCTaPByK
#18 interdiff.txt9.79 KBCTaPByK
#18 dynamic_entity_reference-fix_settings_view_modes_list-2729463-18.patch11.91 KBCTaPByK
#20 interdiff.txt1.46 KBCTaPByK
#20 dynamic_entity_reference-fix_settings_view_modes_list-2729463-20.patch12.63 KBCTaPByK
#22 interdiff.txt2.21 KBCTaPByK
#22 dynamic_entity_reference-fix_settings_view_modes_list-2729463-22.patch12.88 KBCTaPByK

Comments

SylvainM created an issue. See original summary.

sylvainm’s picture

Status: Active » Needs review
StatusFileSize
new1.77 KB

With this patch, the select contains only the view_modes which are enabled for all referencable entities

jibran’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Nice catch. We need tests here.

jibran’s picture

ifrik’s picture

I have a similar problem as described in #2743487: Allow more display modes besides default for "rendered entity" format.
I've tested this patch and it works for me. The dropdown menu now displays all those display modes that the selected entities have in common, including custom ones.

jibran’s picture

Issue tags: +ER feature parity
CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new3.15 KB
new4.92 KB

Patch with test coverage.

The last submitted patch, 7: test-only.patch, failed testing.

CTaPByK’s picture

StatusFileSize
new956 bytes
new2.78 KB
new4.56 KB

Remove unused imports and unnecessary permissions.

The last submitted patch, 9: test-only.patch, failed testing.

slashrsm’s picture

Status: Needs review » Needs work

Great progress! Few things:

  1. +++ b/src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php
    @@ -28,4 +29,36 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
    +    $options = array();
    ...
    +    $available_view_modes = array();
    ...
    +    $elements['view_mode'] = array(
    

    It seems that the short array syntax is used in other classes of the module. Let's be consistent.

  2. +++ b/src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php
    @@ -28,4 +29,36 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
    +    $entity_type_ids = $this->getFieldSetting('entity_type_ids');
    

    We should consider the case where selection is negated ("Exclude" checkbox is enabled).

    Also add test coverage for that.

  3. +++ b/src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php
    @@ -28,4 +29,36 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
    +    foreach ($options as $option_key => $option_value) {
    +      foreach ($available_view_modes as $available_view_mode) {
    +        if (!array_key_exists($option_key, $available_view_mode)) {
    +          unset($options[$option_key]);
    +        }
    +      }
    +    }
    

    I assume that this creates intersection of view modes for all enabled types.

    I think that it would be better to leave all view modes in there and add a description that would explain that the "Default" view mode will be used if the selected one doesn't exist for a given view mode.

    Would also be nice to test this case.

jibran’s picture

Thanks @CTaPByK for the patch and thank you @slashrsm for the review. Yes, please use array short syntax everywhere.
I think we should have per entity type view mode.
node: teaser
user: default
taxonomy_term: term
Having one select field or multi-select field doesn't make much sense imo.

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new4.05 KB
new4.9 KB

Per entity view mode.

Status: Needs review » Needs work
jibran’s picture

Issue tags: -Needs tests
+++ b/src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php
@@ -28,4 +30,22 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
+        '#default_value' => $this->getSetting('view_mode'),

We have to update default setting as well.

jibran’s picture

We also have to override the viewElements to actually use it.

slashrsm’s picture

We also have to override the viewElements to actually use it.

Yes. And if view mode configuration for a given entity type is missing we use default I assume.

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new9.79 KB
new11.91 KB

Default settings is updated also as viewElements and tests.

jibran’s picture

Status: Needs review » Needs work

Thanks for the updated patch only thing remaining is #15 i.e. override defaultSettings in DynamicEntityReferenceEntityFormatter

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new1.46 KB
new12.63 KB

Yes my mistake, now defaultSettings is overridden.

jibran’s picture

Version: 8.x-1.0-rc4 » 8.x-1.x-dev
Status: Needs review » Needs work

Thanks for the patch.

+++ b/src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php
@@ -34,9 +34,28 @@ class DynamicEntityReferenceEntityFormatter extends EntityReferenceEntityFormatt
+    $view_mode = array_fill_keys($options, 'default');
+    return [
+      'view_mode' => $view_mode,
+      'link' => FALSE,
+    ] + parent::defaultSettings();

I think this should be just
return array_fill_keys($options, ['view_mode' => 'default', 'link' => FALSE]). The parent doesn't give us anything so just leave it. Every view mode should have link setting and it is keyed by entity_type. Example can be seen at \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::defaultFieldSettings();.

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new12.88 KB
new2.21 KB

Yes, i change link from bool to sequence of bool, default settings are updated and schema also.

jibran’s picture

StatusFileSize
new4.7 KB
new12.96 KB

I meant like this.

Status: Needs review » Needs work

The last submitted patch, 23: list_of_view_modes_is-2729463-23.patch, failed testing.

CTaPByK’s picture

Oh, ok, then i will fix failing test.

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new5.61 KB
new12.97 KB

Test is fixed i think and settingsSummary method is also fixed.

jibran’s picture

StatusFileSize
new2.35 KB
new13.45 KB

Thanks! for fixing the fails. Almost there. I made some minor string changes. Let's add some asserts related to these in testFieldFormatterViewModes.

  1. +++ b/tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php
    @@ -187,6 +190,64 @@ class DynamicEntityReferenceTest extends JavascriptTestBase {
    +    $assert_session->optionExists('fields[field_foobar][settings_edit_form][settings][user][view_mode]', 'full', $page);
    ...
    +    $assert_session->optionExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', 'teaser', $page);
    

    Let's press update and assert the settings summary.

  2. +++ b/tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php
    @@ -187,6 +190,64 @@ class DynamicEntityReferenceTest extends JavascriptTestBase {
    +    $assert_session->selectExists('fields[field_foobar][settings_edit_form][settings][user][view_mode]', $page);
    ...
    +    $assert_session->selectExists('fields[field_foobar][settings_edit_form][settings][entity_test][view_mode]', $page);
    ...
    +    $assert_session->selectExists('fields[field_foobar][settings_edit_form][settings][entity_test_with_bundle][view_mode]', $page);
    ...
    +    $assert_session->selectExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', $page);
    

    We should also check the labels of the select fields.

Status: Needs review » Needs work

The last submitted patch, 27: list_of_view_modes_is-2729463-27.patch, failed testing.

jibran’s picture

Title: List of view_modes is empty in formatter » List of view_modes is empty in dynamic_entity_reference_entity_view formatter
Issue summary: View changes
Related issues: +#2377841: Allow user to chose per entity type formatter.
CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new14.08 KB
new3.28 KB

Check updating summary and labels are inserted in test. Test is green in my localhost.

Status: Needs review » Needs work

The last submitted patch, 30: list_of_view_modes_is-2729463-30.patch, failed testing.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new1.28 KB
new14.16 KB

Maybe this will fix it.

Status: Needs review » Needs work

The last submitted patch, 32: list_of_view_modes_is-2729463-32.patch, failed testing.

jibran’s picture

Now we have different fails.

CTaPByK’s picture

Yes, i will try to remove this check of default view mode, but however its hard to test when test is pass in my localhost.

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new878 bytes
new14 KB

Test is still green locally, but lets try like this.

Status: Needs review » Needs work

The last submitted patch, 36: list_of_view_modes_is-2729463-36.patch, failed testing.

jibran’s picture

Have you tested it PHP 5.5? I have PHP 7 and it didn't fail for me as well.

CTaPByK’s picture

Im testing also in php 7, but a can switch quickly on 5.5. When i finish i will comment.

CTaPByK’s picture

Im tested with php 5.6 and test is green then i tested with php 5.5 and fails exactly like here, with same message...

CTaPByK’s picture

Status: Needs work » Needs review
StatusFileSize
new2.1 KB
new14.17 KB

I think this is will fix that fails. Actually, ARRAY_FILTER_USE_KEY work from php 5.6.

jibran’s picture

jibran’s picture

Status: Needs review » Needs work
Issue tags: +Needs manual testing, +Needs screenshots

Thanks for fixing the fails.

+++ b/tests/src/Kernel/DynamicEntityReferenceFormatterTest.php
@@ -182,6 +182,7 @@ class DynamicEntityReferenceFormatterTest extends EntityKernelTestBase {
@@ -212,7 +213,12 @@ class DynamicEntityReferenceFormatterTest extends EntityKernelTestBase {

@@ -212,7 +213,12 @@ class DynamicEntityReferenceFormatterTest extends EntityKernelTestBase {
+    $build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, [
+      'view_mode' => [
+        $this->referencedEntity->getEntityTypeId() => 'default',
+        $this->unsavedReferencedEntity->getEntityTypeId() => 'default',
+      ],

These are not correct.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new1.01 KB
new14.25 KB

Fixed #43. I think we should expand this test a little.

  • jibran committed e15b778 on 8.x-1.x authored by CTaPByK
    Issue #2729463 by CTaPByK, jibran, SylvainM, slashrsm: List of...

  • jibran committed b918a4f on 8.x-2.x authored by CTaPByK
    Issue #2729463 by CTaPByK, jibran, SylvainM, slashrsm: List of...
jibran’s picture

Issue summary: View changes
Status: Needs review » Fixed
Issue tags: -Needs manual testing, -Needs screenshots
StatusFileSize
new2.64 KB
new15.11 KB
new18.76 KB

I update the format summary text a bit, did some manual testing. Here are the screenshots.
Formatter settings

Formatter rendring

Thank you! committed and pushed to both branches.

Status: Fixed » Closed (fixed)

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

jibran’s picture

The is an upgrade path for this change in open open_social. https://github.com/goalgorilla/open_social/blob/8.x-1.x/modules/social_f...