Problem/Motivation
By default, content (nodes) has a default value for the meta description, [node:summary]. However, that default does not appear in the metatag field when adding or editing content, even though the default does appear when viewing the content and viewing source. This discrepancy leads to confusion when adding and editing content.
To reproduce:
- Install the metatag module.
- Observe that in admin/config/search/metatag, under CONTENT, the description has a default of [node:summary].
- Add the metatag field to a content type.
- Create a new node of that type.
- Expand the metatag field, and observe that the description field is empty. Expected: it should match the configured default for content, [node:summary].
- Note that if you leave the description field empty and save the node, then view the node and view source, the meta description is populated correctly.
- Note also that if you edit the node and expand the metatag field, the description field is still empty. Expected: it should match the configured default.
Proposed resolution
When a default is configured, the default should appear in the appropriate metatag field when adding or editing content, if the default has not been overridden.
Remaining tasks
TBD
User interface changes
TBD
API changes
TBD
Comments
Comment #2
steffenrI did some debugging on this issue.
It's more an UI display issue - calling the node page directly shows the correct default values provided for a given entity type.
Even if you enter some custom values in the metatags field, those values are correct on the node (view) page.
The problem is, that the metatag_get_route_entity function does not return an EntityInterface cause the route node.add is not processed correctly in here.
Comment #3
yannickooThank you for the hint Steffen! I managed it to get this working for nodes but I'm not sure whether this could work for other bundable entity types because of the
'/(.*)\.add/'route, I doubt that other developers use always that pattern for creating an entity with a specific bundle.Comment #5
agentrickardPatch works as designed for new content. I wonder why the test is failing?
Comment #6
damienmckennaI think the tests should confirm the fields are present on the form.
Comment #7
agentrickardLooking at that now.
Comment #8
agentrickardI have looked for a half hour and have no idea why this test would fail. It can't find the "entity_test" entity type.
Comment #9
damienmckennaComment #10
it-cruWith this patch enabled with beta9 we can't create taxonomy terms any more.
Comment #12
damienmckennaRerolled.
Comment #14
damienmckennaOk, changing MetatagFieldTest so that every drupalPostForm() call is preceded by a drupalGet() to confirm the form loads in the first place.
Comment #16
damienmckennaSo the failure is loading the form in the first place, which suggests that the patch breaks something in how the "entity_test" entity is loaded.
Comment #17
damienmckennaIt's a little kludgy, but I'm not sure what the appropriate solution is. This removes occurrences of "entity." from the $entity_type string, but now results in:
Fatal error: Call to a member function bundle() on null in metatag.module on line 179Comment #19
damienmckennaComment #21
damienmckennaThis doesn't fully resolve the problem anyway, because the form doesn't overwrite global values unless they're empty, e.g.:
Comment #22
damienmckennaThis is causing the tests in #2775441: Replace deprecated entity_load and entity_create with appropriate methods to fail.
Lets fix this for the next release.
Comment #23
damienmckennaSkipping the $type variable if it's empty.
Comment #24
damienmckennaMoving the (minor) test changes into #2780109: Update all tests so drupalGet() runs before drupalPostForm().
Comment #25
damienmckennaJust the changes to metatag.module.
Comment #27
damienmckennaI've committed this so that I can then commit #2775441: Replace deprecated entity_load and entity_create with appropriate methods.
Comment #28
damienmckenna.. but this still needs more work and tests to confirm that the correct default values are being loaded.
Comment #29
damienmckennaThese tests should pass, but they won't.
Comment #31
damienmckennaThis happens because of the usage of array_merge() to combine the various levels of defaults. So that needs to be fixed.
Comment #32
damienmckennaSo in formElement() on a node/add/article form (which has a Metatag field) it has already loaded the global values into the $items object. Not sure why this is happening, anyone have any ideas?
Comment #34
damienmckennaAh! I dug into the data in the {config} table and it seems that the global defaults are being saved into field.field.[entitytype].field_[fieldname] in default_value[0]['value']. So that's the problem.
Comment #35
damienmckennaLets see if this fixes it - it changes metatag_form_field_config_edit_form_alter() so that it sets $form['default_value']['widget'] to NULL, hopefully blocking out any default values from being saved.
Comment #37
damienmckennaLets see if this works.
Comment #39
damienmckennaSomehow changing the defaults to NULL is making it throw an "Unsupported operand types" error in getFieldTags(). Lets try an empty array as the default value instead of NULL.
Comment #40
damienmckennaSo, maybe we just need to skip the unserialize() step if the string is empty?
Comment #41
damienmckennaA slightly better approach - instead of erasing the entire default_value structure, just erase each individual '#default_value' value.
Comment #42
damienmckennaPeople who are more familiar with the D8 APIs - is #41 a reasonable approach to this problem, where we don't actually want to store a field's default values at the field level so that it may be inherited from the global configurations instead?
Comment #44
damienmckennaCommitted. Thanks all!
Comment #47
drupalninja99 commented@Damien, I have the latest beta but I still see this problem on a clean install. I am getting the global tokens instead of the content tokens.
Comment #48
khaled.zaidan commentedYeah, unfortunately, same here, as well... seeing global tokens instead of content tokens :/
Edit: Actually I just tried (and looked at the code of) the dev version, and this has been fixed :)
Comment #49
damienmckennaPlease test the latest -dev release, this was fixed in #2817309: Metatag shows predefined global values every time in forms (with tests!).