Problem/Motivation

Let's assume one has a entity reference field which allows you to select different kind of bundles. This is quite helpful in the context of media for example.
It would be kind of helpful to have a formatter which allows you to choose per bundle which exact formatter + their settings should be chosen.

Proposed resolution

Add one.

Remaining tasks

  • Review
  • Address feedback
  • Commit

User interface changes

API changes

Data model changes

Comments

dawehner created an issue. See original summary.

jibran’s picture

dawehner’s picture

Assigned: Unassigned » dawehner

Assigning to myself to make it clear that I'm working on that issue, because, well, we need it.

@jibran
If we can make those formatters working for both ER, ERR and DER at least for myself I'd be happy to support it.

dawehner’s picture

Assigned: dawehner » Unassigned
Status: Active » Needs review
StatusFileSize
new17.16 KB

Here is a patch with a test included

Status: Needs review » Needs work

The last submitted patch, 4: 2813277-2.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new17.47 KB
new748 bytes

Here is a patch which provides also a settings summary.

Status: Needs review » Needs work

The last submitted patch, 6: 2813277-6.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new17.45 KB
new663 bytes

.

Status: Needs review » Needs work

The last submitted patch, 8: 2813277-8.patch, failed testing.

slashrsm’s picture

Issue tags: +D8Media

This looks great and very useful!

  1. +++ b/config/schema/field_formatter.schema.yml
    @@ -24,6 +24,20 @@ field.formatter.settings.field_formatter_with_inline_settings:
    +field.formatter.settings.field_formatter__per_bundle:
    +  type: ignore
    +#  type: mapping
    +#  mapping:
    +#    formatter:
    +#      type: sequence
    +#      sequence:
    +#        type: mapping
    +#        mapping:
    +#          type:
    +#            type: string
    +#          settings:
    +#            type: any
    

    Can we have something better for schema?

  2. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +      $build[$delta] = $this->getViewDisplay($entity->bundle())->build($fake_entity);
    

    Would it make sense to use just the first item? I am not sure but I suspect that we might be able to get rid of a div in that case.

  3. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +    $settings[$bundle_id] += [
    +      'settings' => [],
    +    ];
    +    if (isset($settings[$bundle_id])) {
    +      return $settings[$bundle_id];
    +    }
    

    What will happen if !isset($settings[$bundle_id]) and we set this?

    I tried in Psy shell and got segmentation fault back into my face...

  4. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +  protected static function getChangedFieldAndBundle(FormStateInterface $form_state) {
    

    Comment missing for this function.

  5. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +    // Determine on which bundle this change was.
    +    // We look at the #name of the submitted formatter. In case there are better
    +    // ways, please let us know.
    

    Could you use #parents?

    Also use all 80 characters.

  6. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +    $handler_settings = $this->fieldDefinition->getSetting('handler_settings');
    +    if (empty($handler_settings['target_bundles'])) {
    +      return $form;
    +    }
    +
    +    foreach ($handler_settings['target_bundles'] as $bundle) {
    

    Should we use similar approach as in #2813913: Support Entity reference selection configuration without any target bundles?

  7. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +  protected function getSettingFromFormState(FormStateInterface $form_state, $bundle, $setting) {
    

    Missing comment block.

  8. +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterPerBundle.php
    @@ -0,0 +1,347 @@
    +    if ($setting == 'settings') {
    +      return [];
    +    }
    

    In which case this happens? Maybe add a comment?

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new18.09 KB
new5.32 KB

Thank you for the review @slashrsm!

Would it make sense to use just the first item? I am not sure but I suspect that we might be able to get rid of a div in that case.

Well, can't the entity reference contains multiple entities in the first place?

I tried in Psy shell and got segmentation fault back into my face...

So the current solution is okay?

Could you use #parents?

Maybe ^^, indeed this works much better.

Should we use similar approach as in #2813913: Support Entity reference selection configuration without any target bundles?

Good catch

In which case this happens? Maybe add a comment?

I did

slashrsm’s picture

Almost there :).

Well, can't the entity reference contains multiple entities in the first place?

That code hunk is always rendering a single entity. Doesn't this mean that we're adding an additional wrapper for each of them?

So the current solution is okay?

I don't think so. It is current solution that segfaulted on me when testing it.

dawehner’s picture

That code hunk is always rendering a single entity. Doesn't this mean that we're adding an additional wrapper for each of them?

Well, right, but we still iterate over the referenced entities. Maybe I'm just not sure what exactly you are talking about :)

I don't think so. It is current solution that segfaulted on me when testing it.

OH

anybody’s picture

Status: Needs review » Needs work

> 3 years old. This issue now needs a reroll against latest 1.x-dev. Otherwise please close as outdated.

anybody’s picture

Version: 8.x-1.x-dev » 3.x-dev

Switched to 3.x!

@dawehner still interested to reroll this?

anybody’s picture

Issue tags: -D8Media +Needs tests

And could someone please prepare this as MR? Furthermore it needs tests.

anybody’s picture

@dawehner & @slashrsm still interested to implement this?
If yes, please use a MR.

anybody’s picture

Status: Needs work » Closed (outdated)

Ok let's close this outdated then. If anyone needs it, I'd vote to put it into an optional submodule, so we don't blow up complexity for regular cases.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.