Problem/Motivation

The most basic use case for referencing a media entity from other entities (nodes, users, etc) is through an Entity Reference field.
By default, this field outputs the referenced entity with the "Label" formatter. That's unlikely the preferred option for most users who reference media items from other entities.

Proposed resolution

Hook into the field creation process and define the display to use the "Rendered entity" formatter whenever the field being created is referencing media entities.

Remaining tasks

User interface changes

API changes

Introducing new alter hook hook_field_ui_preconfigured_options_alter

Data model changes

Comments

marcoscano created an issue. See original summary.

marcoscano’s picture

Assigned: Unassigned » marcoscano

Working on it.

marcoscano’s picture

Assigned: marcoscano » Unassigned
Status: Active » Needs review
StatusFileSize
new2.05 KB

First shot, highly inspired in something similar Lightning does for field widgets :)

phenaproxima’s picture

Issue tags: +Needs tests

Looks great to me! Just needs tests.

seanb’s picture

+++ b/core/modules/media/media.module
@@ -96,3 +98,42 @@ function template_preprocess_media(array &$variables) {
+  $fields = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($display->getTargetEntityTypeId());
+  $fields = array_filter($fields, function (FieldStorageDefinitionInterface $field) {
+    return $field->getType() == 'entity_reference' && $field->getSetting('target_type') === 'media';
...
+  if (isset($display->original)) {
+    $new_components = array_diff_key($display->getComponents(), $display->original->getComponents());

I'm just wondering, can we change the order of checking for new fields and entity reference field of type media?

I think the check for media fields is a little more expensive. We could probably return faster when there are no new components.

marcoscano’s picture

Issue tags: -Needs tests
StatusFileSize
new4.37 KB
new3.66 KB

Thanks for reviewing!
This should address #4 and #5.

amateescu’s picture

  1. +++ b/core/modules/media/media.module
    @@ -96,3 +98,42 @@ function template_preprocess_media(array &$variables) {
    +    return $field->getType() == 'entity_reference' && $field->getSetting('target_type') === 'media';
    

    In general we try not to hardcode the 'entity_reference' field type, but use the class and its descendants instead. See field_field_config_presave() for an example.

  2. +++ b/core/modules/media/media.module
    @@ -96,3 +98,42 @@ function template_preprocess_media(array &$variables) {
    +      'settings' => [
    +        'view_mode' => 'default',
    +        'link' => FALSE,
    +      ],
    

    These are automatically provided by setComponent() so they can be removed.

  3. +++ b/core/modules/media/media.module
    @@ -96,3 +98,42 @@ function template_preprocess_media(array &$variables) {
    +      'region' => 'content',
    

    Is there any reason for not using the region that's already configured in the view display?

amateescu’s picture

However, there is a much bigger problem with the current patch: doing it like this in a presave hook means that we always override the formatter for a newly added component, even when some code sets a specific formatter via ->setComponent($field_name, ['type' => 'my_custom_formatter']).

Which makes we wonder.. why are we not doing what was proposed in #2831943-123: Use "rendered media" (not links) as default media field formatter; add modal to configure the used media view mode instead?

marcoscano’s picture

StatusFileSize
new10.59 KB

Oh that's true, thanks for pointing it out.

Starting over then, with the approach of the alter hook.

As far as I could check, the alter hook with the results of \Drupal\Core\Field\FieldTypePluginManager::getUiDefinitions as suggested isn't enough, because the entity view display is being configured in \Drupal\field_ui\Form\FieldStorageAddForm::submitForm with the direct results from getPreconfiguredOptions. Firing the hook always after we call getPreconfiguredOptions still allows the module alterations to be preserved for getUiDefinitions though.

The attached patch seems to work fine for me. No interdiff because it's completely different from #6.

Thanks!

phenaproxima’s picture

I like the overall approach in #9. However, I wonder if we need to do it by way of yet another alter hook.

What about this -- could we add a 'common_reference_options' array to the Media entity type annotation, and change EntityReferenceItem::getPreconfiguredOptions() to use that instead?

That way, the common options are still alterable in hook_entity_type_build() or hook_entity_type_alter(), and we don't need to introduce a new hook.

berdir’s picture

Yes, in the pargraphs module, we have this in the annotation:

*   default_reference_revision_settings = {
 *     "field_storage_config" = {
 *       "cardinality" = -1,
 *       "settings" = {
 *         "target_type" = "paragraph"
 *       }
 *     },
 *     "field_config" = {
 *       "settings" = {
 *         "handler" = "default:paragraph"
 *       }
 *     },
 *     "entity_form_display" = {
 *       "type" = "entity_reference_paragraphs"
 *     },
 *     "entity_view_display" = {
 *       "type" = "entity_reference_revisions_entity_view"
 *     }
 *   }

And that just works, thanks to \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem::getPreconfiguredOptions. The only problem is with re-using existing fields, but there is an issue for that: #2717319: Provide better default configuration when re-using an existing field.

amateescu’s picture

To be honest, I'd prefer the hook approach. #11 shows that's quite a lot of stuff to put in the entity annotation. But I don't have any strong feelings about it so I'll defer to whatever is preferred by the majority :)

phenaproxima’s picture

#11 shows that's quite a lot of stuff to put in the entity annotation.

Yes, in the worst-case scenario. But I imagine that most uses of this functionality would be for very small tweaks that would not add much to the entity annotation at all. So I think it might ultimately be less verbose than a hook.

phenaproxima’s picture

Also, to be clear, the approach I suggest can still be hook-based (hook_entity_type_build() and hook_entity_type_alter()). I simply don't favor the idea of adding a new hook just for this. But I can probably be convinced otherwise.

marcoscano’s picture

I am no-one to give a valuable opinion :), but I think I would also prefer the alter-hook approach, with the purpose of being more explicit. The new hook is documented in field.api.php, parsed by IDEs, docs APIs, etc, and clearly indicates what you can do with it. It seems to me that the annotation-based alternative + "generic" hook is harder to discover if you don't know the trick already.

chr.fritsch’s picture

I don't favor to add a new hook for this, too. And I would go with the annotations if it doesn't blow them up heavily.

seanb’s picture

For most media sources there would be no special settings, for the cases there are, having some extra annotations would be acceptable for me. It's very clear to have everything together.

yoroy’s picture

Looking at https://www.drupal.org/files/issues/referencing_media_entities.mp4 this is shaping up very nicely.

For an even better default image media view mode I would suggest to order media before name. First show the cat, then show its name :)

marcoscano’s picture

So it seems the majority prefers the annotation-based approach suggested in #10, which is OK for me then :)

However, the same alter hook proposed here would also solve #2862458: [META] Once media is enabled, having the File, Image and Media reference fields all listed is confusing, as shown in #2862458-61: [META] Once media is enabled, having the File, Image and Media reference fields all listed is confusing. I don't know any other alternative way of solving that issue (i.e. having a specific description for the "Media" field on the UI), without this alter hook. Maybe you can help me figure out a way? If so please let me know, I have some availability to move these 2 issues forward with the new approach(es) in the next days.

Thanks!

marcoscano’s picture

Re: #18:

For an even better default image media view mode I would suggest to order media before name. First show the cat, then show its name :)

Thanks @yoroy for the feedback!

The order of the fileds (title + image) however is part of the default config shipped in the standard profile (not something we are modifying here). In order to change that, IMHO it would make more sense to do it in a separate issue. I have opened #2930788: Do not show name by default in media displays for that.

Thanks!

yoroy’s picture

thank you @marcoscano, that's very much its own issue then indeed.

marcoscano’s picture

This conversation happened in slack:

marcoscano [4:11 PM] 
Friends, I'd like to move https://www.drupal.org/project/drupal/issues/2928699#comment-12384133 forward as much as possible, while I can dedicate time to it (today/tomorrow), because this appears to be an important piece to possibly "downgrade" the ex-formatter's issue (2831943) from our list of blockers. (edited)

I'm OK to proceed with the annotation-based approach @phenaproxima proposed, but I'm not sure then how to solve the other issue this same hook would solve. It would be sad to fix this with the annotation solution and then end up having to create the hook for the other issue. Ideas?

seanb [4:18 PM] 
Using the hook for https://www.drupal.org/project/drupal/issues/2862458#comment-12382165 as well already felt a little weird

Can't we just do a form alter or something?

marcoscano [4:23 PM] 
To be honest, I wasn't considering a standard form_alter... :confused: but I didn't have a strong reason for that. It's an alter hook at the end of the day. It appeared to me that, generically speaking,  `PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()` was something useful for being exposed for alteration, and these 2 examples show use cases for that.

But it's true that if we can get away with a form_alter, we may avoid all that. The code will be a lot more hackish though, but I think it may get the job done.

seanb [4:26 PM] 
@marcoscano True. Let me check again

marcoscano [4:30 PM] 
@seanb maybe the underlying issue here is: "Should we expose `PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()` to be altered by modules" in its own issue, and have these 2 issues postponed on this decision, as examples of the need. If we end up deciding it's not worth, then we look for alternative approaches to solve the issues on their own.

I was refraining from opening a new one because it will delay everything a little more, but maybe it's the right thing to do™️

seanb [4:39 PM] 
@marcoscano From that perspective the alter hook makes a little more sense than the annotations. Changing the default form / view display is not a media specific issue.

@marcoscano Adding that description feels kind of hacky as well though

marcoscano [4:42 PM] 
@seanb true, but the goal is legitimate. Having "`An entity field containing an entity reference.`" as description for the Media field on the UI feels wrong, and all pre-configured entity_reference fields inherit this description.

[4:43] 
And they don't have an annotation on their own because they're not plugins, just pre-configured ui fields :) (as `PreconfiguredFieldUiOptionsInterface` defines them)

seanb [4:43 PM] 
@marcoscano Extending the fieldtype is a bit overkill to add a description, but as a solution it might be more clear.

marcoscano [4:44 PM] 
Oh I have a strong feeling this would never have committers sign-off. it would mean to create new fieldtypes for nodes, terms, comments, users, media, etc, only for their Labels/Descriptions...

seanb [4:45 PM] 
@marcoscano I also doubt adding a description nobody reads will fix the actual issue. The confusion is caused showing the first dropdown. Letting people know afterwards "yo, watch out you might have made the wrong choice" is not a fix

[4:45] 
@marcoscano Yeah, just thinking out loud

@marcoscano tbh the description shouldn't even be nessecary

marcoscano [4:46 PM] 
@seanb I think it is indeed optional in the annotation. But UX-wise the conclusion seems to be that the descriptions will solve the confusion behind https://www.drupal.org/project/drupal/issues/2862458 (edited)

Or at least will reduce it enough so we can call it fixed just by exposing the descriptions on the UI

seanb [4:48 PM] 
@marcoscano "I'm presented a bunch of choices, and File and Media are two separate choices within the "Reference" section. How do I know how these differ?"

@marcoscano That is still the issue though

marcoscano [4:49 PM] 
@seanb not as much. We discussed this live on the UX meeting this week. By having the descriptions show each time you select one of these, it's way more clear what each should be used for

(provided we have good wording on these descriptions)

seanb [4:52 PM] 
In the end you will learn, true..

So now the issue is, where to store the descriptions..

seanb [4:54 PM] 
@marcoscano I'm not sure. The alter hook feels hacky, a form alter as well. Putting it in the field type is the best option but since we don't have field types for everything this is a lot of overkill.

marcoscano [4:56 PM] 
@seanb yes, I agree. Still think though that maybe it will be easier to decide if the separate question _"Should we expose PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions() to be altered by modules"_ is thought separately in a generic context.

seanb [4:58 PM]
Ok, let's propose the hook first since that could also solve the annotations related stuff and who know what other use cases. Changing the default form/view display for entity references doesn't sound like an edge case at all.

marcoscano [13 minutes ago] 
OK. Would you mind if I move parts of this conversation to the issue and try to sneak the hook in there?

seanb [8 minutes ago] 
Please go ahead

TL;DR;

Allowing modules be able to alter the output of PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions() would potentially be a good addition in a generic context. As Sean said above, and these two issues (this one and #2862458) demonstrate, there are legitimate use cases for that.

Under that perspective, could we give the hook_alter approach another chance? :)

amateescu’s picture

As I said before, I'm a fan of the alter hook :) I should have done it myself when I wrote the "preconfigured options" stuff years ago, but there were too many things to do/fix at that time..

seanb’s picture

Status: Needs review » Reviewed & tested by the community

I'm convinced, let go with the alter hook. It's useful for at least 2 media related issue at the moment. As mentioned in slack, changing the default form/view display for entity references doesn't sound like an edge case at all. Thanks for taking the time to discuss this marcoscano!

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work

This looks good and straightforward; just a few small cleanliness things.

  1. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,24 @@ function hook_field_info_alter(&$info) {
    + * @param array $options
    + *   Array of options as retrieved from ::getPreconfiguredOptions().
    + *
    + * @see \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()
    + */
    +function hook_field_preconfigured_options_alter(array &$options, array $field_definition) {
    

    $field_definition should also be mentioned in the doc block.

  2. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -312,14 +312,19 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +        \Drupal::moduleHandler()->alter('field_preconfigured_options', $options, $field_definition);
    

    Is it possible to inject the module handler?

  3. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -349,8 +356,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +        $this->configureEntityFormDisplay($values['field_name'], $widget_options);
    +        $formatter_options = $formatter_id ? ['type' => $formatter_id] : [];
    

    Nit: Can there be a blank line between these two "paragraphs"?

  4. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -416,14 +431,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +  protected function configureEntityFormDisplay($field_name, $options = []) {
    

    $options should be type hinted.

  5. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -434,14 +449,14 @@ protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
    +  protected function configureEntityViewDisplay($field_name, $options = []) {
    

    Ditto.

  6. +++ b/core/modules/media/media.module
    @@ -96,3 +96,22 @@ function template_preprocess_media(array &$variables) {
    +  if (!empty($field_definition['id']) && $field_definition['id'] === 'entity_reference') {
    

    Under what circumstances would $field_definition['id'] be empty?

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new12.28 KB
new6.71 KB

Thanks for reviewing @phenaproxima!

This should address all points from #25.

Status: Needs review » Needs work

The last submitted patch, 26: interdiff-9-26.patch, failed testing. View results

marcoscano’s picture

Status: Needs work » Needs review

Sorry, the interdiff is not a patch :/

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Kickass. Back to the land of RTBC we go.

amateescu’s picture

Status: Reviewed & tested by the community » Needs work

Very nice progress, this is looking pretty good already. Here's a few points:

  1. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
    @@ -647,8 +647,8 @@ public static function getPreconfiguredOptions() {
    -          ]
    -        ]
    +          ],
    +        ],
    

    Unrelated changes :)

  2. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,25 @@ function hook_field_info_alter(&$info) {
    + *   Array of options as returned from ::getPreconfiguredOptions().
    

    We need to use the fully qualified method name here, ::someMethodName() doesn't make too much sense in the context of an .api.php file.

  3. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,25 @@ function hook_field_info_alter(&$info) {
    + * @param array $field_definition
    + *   A field plugin definition array, as returned from
    + *   \Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition().
    

    I'm not sure it's very useful to pass the entire $definition array, I think passing the field type is enough.

  4. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,25 @@ function hook_field_info_alter(&$info) {
    +function hook_field_preconfigured_options_alter(array &$options, array $field_definition) {
    

    "Pre-configured options" is a concept of the Field UI module, not the generic Field system, so the hook name should be hook_field_ui_preconfigured_options_alter.

  5. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,25 @@ function hook_field_info_alter(&$info) {
    +  if ($field_definition['id'] === 'entity_reference' && !empty($options['media'])) {
    

    Like I mentioned previously, we shouldn't hardcode the name of the entity reference field type, but get the actual class of the field type and check if the class is an instance of EntityReferenceItem.

    See field_field_config_presave() for an example.

  6. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -55,6 +56,14 @@ class FieldStorageAddForm extends FormBase {
    +  protected $moduleHandler;
    +
    +
    +  /**
    

    Extra empty line here.

  7. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -312,14 +325,19 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +        $this->moduleHandler->alter('field_preconfigured_options', $options, $field_definition);
    

    Not very happy that we need to fire the alter hook in two places.. this probably means we should have a method on FieldTypePluginManagerInterface which gets the pre-configured options for a field type and, fires the alter hook and then returns the result.

  8. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -416,14 +445,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    -   * @param string|null $widget_id
    -   *   (optional) The plugin ID of the widget. Defaults to NULL.
    +   * @param array $options
    +   *   (optional) An array of options for this component. Defaults to an empty
    +   *   array.
    ...
    -  protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
    +  protected function configureEntityFormDisplay($field_name, array $options = []) {
    

    How about sending a new $widget_settings parameter instead?

    Same for configureEntityViewDisplay().

Also, we should have dedicated test coverage for the new hook. We already have a dedicated field type for testing pre-configured options (\Drupal\field_test\Plugin\Field\FieldType\TestItemWithPreconfiguredOptions), so we should simply be able to alter that by implementing a hook in field_test.module and test it in \Drupal\field_ui\Tests\ManageFieldsTest::testPreconfiguredFields.

marcoscano’s picture

Status: Needs work » Needs review

Thanks @amateescu for reviewing!

1. Fixed (Feeling sad while doing so, but fixed :)
2. Fixed
3. Fixed (However I feel that passing the whole definition would give implementers more context? For example, it could perhaps help in solving point 5 below in a cleaner way? e.g. if (is_subclass_of($definition['class'], 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem')) { ...) But I don't feel strongly about it, so I just changed it anyway.
4. Fixed
5. Fixed
6. Fixed
7. Fixed (Please feel free to suggest a better name for the new method, not quite happy with it but can't think of anything better.)
8. Fixed
9. Fixed. (I'm having some troubles with simpletest in my local so I couldn't verify the new test, let's see what the testbot says about it. Sorry for that if it doesn't pass.)

marcoscano’s picture

StatusFileSize
new17.2 KB
new14.85 KB

And now with the files...

Status: Needs review » Needs work

The last submitted patch, 32: 2928699-31.patch, failed testing. View results

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new17.2 KB
new702 bytes

Fewer failures should happen now

amateescu’s picture

Status: Needs review » Needs work

Thanks, @marcoscano, this looks much better now! Here's a few more points that could be improved:

  1. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
    @@ -85,6 +85,30 @@ public function getDefaultStorageSettings($type);
    +   * Returns preconfigured field options for a field type, after being altered.
    ...
    +   *   and after being exposed for alteration by modules.
    ...
    +  public function getAlterablePreconfiguredOptions($class, $field_type);
    

    We don't really mentioned anywhere in core that a method invokes an alter hook, so I think we should remove all the mentions of 'alterable' from the method name and its documentation.

  2. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
    @@ -85,6 +85,30 @@ public function getDefaultStorageSettings($type);
    +   * @param string $class
    +   *   The class implementing
    +   *   \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions().
    

    Is there any reason to have the field type class as an argument here? We're in the field type plugin manager so we can easily get the field type definition from its ID.

  3. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
    @@ -85,6 +85,30 @@ public function getDefaultStorageSettings($type);
    +   *   The field type plugin id.
    

    We need to capitalize 'id' here: ".. plugin ID."

  4. +++ b/core/lib/Drupal/Core/Field/PreconfiguredFieldUiOptionsInterface.php
    @@ -16,6 +16,11 @@
    +   * Note that if you want to give modules an opportunity to alter the result
    +   * of this method, you should call
    +   * \Drupal\Core\Field\FieldTypePluginManagerInterface::getAlterablePreconfiguredOptions()
    +   * instead.
    

    As a continuation to the point above, this documentation is actually useful, so let's keep it :)

  5. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,35 @@ function hook_field_info_alter(&$info) {
    +  if ($class !== $item_class && !is_subclass_of($class, $item_class)) {
    

    Instead two conditions we can use is_a() here.

  6. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -4,6 +4,7 @@
    +use Drupal\Core\Extension\ModuleHandlerInterface;
    
    @@ -55,6 +56,13 @@ class FieldStorageAddForm extends FormBase {
    +  protected $moduleHandler;
    
    @@ -63,11 +71,14 @@ class FieldStorageAddForm extends FormBase {
    -  public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, ConfigFactoryInterface $config_factory) {
    +  public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
    ...
    +    $this->moduleHandler = $module_handler;
    
    @@ -84,7 +95,8 @@ public static function create(ContainerInterface $container) {
    +      $container->get('module_handler')
    

    Since the alteration is done by the field type manager, we don't need to inject the module handler anymore here.

  7. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -418,12 +435,20 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +        'settings' => $widget_settings,
    
    @@ -436,12 +461,20 @@ protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
    +        'settings' => $formatter_settings,
    

    Adding $widget_settings to this array should be handled in its own if condition, based whether a non-empty array was received by the method.

  8. +++ b/core/modules/media/media.module
    @@ -96,3 +96,23 @@ function template_preprocess_media(array &$variables) {
    +  if ($class !== $item_class && !is_subclass_of($class, $item_class)) {
    

    Same as above, let's use is_a().

  9. +++ b/core/modules/media/tests/src/Functional/MediaEntityReferenceFieldTest.php
    @@ -0,0 +1,29 @@
    +  public function testRenderedEntityReferencedMedia() {
    

    Can't we put this new test inside the existing \Drupal\Tests\media\Functional\MediaUiFunctionalTest test class?

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new14.83 KB
new12.51 KB

Thanks @amateescu for reviewing!

This should address #35.

phenaproxima’s picture

Status: Needs review » Needs work

Looks quite good! Just a few very small things.

  1. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
    @@ -85,6 +85,26 @@ public function getDefaultStorageSettings($type);
    +   * allowing modules to alter the result of this method by implementing the
    +   * alter hook "hook_field_ui_preconfigured_options_alter()".
    

    I don't think we need to say "...the alter hook". We can just say "implementing hook_field_ui_preconfigured_options_alter()".

  2. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,34 @@ function hook_field_info_alter(&$info) {
    + * Perform alterations on field preconfigured options.
    

    Should say "preconfigured field options".

  3. +++ b/core/modules/field/field.api.php
    @@ -59,6 +59,34 @@ function hook_field_info_alter(&$info) {
    + *   The id of the field type plugin implementing
    

    Can this just say "The field type plugin ID"?

  4. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -418,12 +422,20 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    +      if (!empty($widget_settings)) {
    +        $options['settings'] = $widget_settings;
    +      }
    

    Why do we need the if (!empty()) check?

  5. +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
    @@ -436,12 +448,20 @@ protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
    +      if (!empty($formatter_settings)) {
    +        $options['settings'] = $formatter_settings;
    +      }
    

    Same here -- why do we need to check if $formatter_settings is not empty?

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new14.74 KB
new1.6 KB

Thanks @phenaproxima for reviewing!

Re: #36.4 and #36.5, unless I misunderstood #35.7, the idea is to avoid unnecessarily adding a 'settings' key with an empty array?

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Okay, cool. I re-read the patch and can find nothing to complain about :) Let's go back to RTBC.

amateescu’s picture

Title: Show referenced media as "Rendered entity" by default, instead of using default formatter value ("Label") » Add an alter hook for the pre-configured field UI options and implement it in the Media module
Component: media system » field system

Looks good to me as well now. Great work, Marcos!

I'm changing some metadata for this issue because the actual Media part is just the hook implementation. This makes me wonder if we also need a change record..

phenaproxima’s picture

Issue tags: +Needs change record

We're adding a hook, so I suspect we do.

marcoscano’s picture

Created the draft change record: https://www.drupal.org/node/2932468

Never wrote a CR before, so please feel free to indicate any adjustments needed :)

Thanks!

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs change record
  1. +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
    @@ -135,7 +135,8 @@ public function getUiDefinitions() {
    +        $options = $this->getPreconfiguredOptions($definition['id']);
    +        foreach ($options as $key => $option) {
    

    do we need the $options local variable here, can we just inline it?

  2. +++ b/core/modules/media/media.module
    @@ -96,3 +96,22 @@ function template_preprocess_media(array &$variables) {
    +    $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view';
    

    Do we want to add settings here too?

marcoscano’s picture

StatusFileSize
new14.71 KB

@larowlan thanks for reviewing!

1. Fixed

2. We don't really need to. The "default" viewmode for media entities is already what we want to render, in most cases, so it's fine to just use the default settings. I originally thought of doing something like:

+    if (!empty($options['media'])) {
+      $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view';
+      // @TODO Do we want to tweak settings here as well? These are just the
+      // defaults, but setting something different here works as well.
+      $options['media']['entity_view_display']['settings'] = [
+        'view_mode' => 'default',
+        'link' => FALSE,
+      ];
+    }

just as an example for developers, but then realized it doesn't make much sense to add this there if it's not strictly needed.

Did you have something else in mind?

Thanks!

marcoscano’s picture

StatusFileSize
new871 bytes

Forgot the interdiff for the patch in #44, sorry.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Testbot is happy, so back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2928699-44.patch, failed testing. View results

marcoscano’s picture

Status: Needs work » Reviewed & tested by the community

Testbot glitch

vijaycs85’s picture

Issue summary: View changes
Issue tags: +Needs change record

+1 to RTBC. As the issue introducing new alter hook, we might need a change record?

marcoscano’s picture

Issue tags: -Needs change record

Thanks @vijaycs85!

There is a CR proposal in #42 , please feel free to comment on it if any adjustment is needed!

vijaycs85’s picture

my bad, CR looks good.

phenaproxima’s picture

phenaproxima’s picture

catch’s picture

Quick note: +1 to the hook over the large annotation.

larowlan’s picture

Adding review credits for @yoroy (manual testing with screencast), @phenaproxima and @amateescu (reviews) and @seanB (clearly involved in the design and scope as evidenced by IRC conversation)

  • larowlan committed cf805af on 8.5.x
    Issue #2928699 by marcoscano, phenaproxima, amateescu, seanB, yoroy: Add...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed as cf805af and pushed to 8.5.x.

Unpostponed the postponed issue.

Published the change record.

Status: Fixed » Closed (fixed)

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