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:

  1. Install the metatag module.
  2. Observe that in admin/config/search/metatag, under CONTENT, the description has a default of [node:summary].
  3. Add the metatag field to a content type.
  4. Create a new node of that type.
  5. Expand the metatag field, and observe that the description field is empty. Expected: it should match the configured default for content, [node:summary].
  6. 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.
  7. 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

paulmckibben created an issue. See original summary.

steffenr’s picture

I 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.

yannickoo’s picture

Status: Active » Needs review
StatusFileSize
new1.31 KB

Thank 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.

Status: Needs review » Needs work

The last submitted patch, 3: metatag-2748615-empty-defaults-3.patch, failed testing.

agentrickard’s picture

Patch works as designed for new content. I wonder why the test is failing?

damienmckenna’s picture

I think the tests should confirm the fields are present on the form.

agentrickard’s picture

Looking at that now.

Fatal error: Call to a member function id() on a non-object in /Applications/MAMP/htdocs/drupal-8/modules/metatag/src/Tests/MetatagFieldTest.php on line 113
FATAL Drupal\metatag\Tests\MetatagFieldTest: test runner returned a non-zero error code (255).
Drupal\metatag\Tests\MetatagFieldTest                          0 passes   1 fails                            
- Found database prefix 'simpletest458607' for test ID 29.
[09-Jul-2016 01:05:19 Australia/Sydney] Uncaught PHP Exception Drupal\Component\Plugin\Exception\PluginNotFoundException: "The "entity.entity_test" entity type does not exist." at /Applications/MAMP/htdocs/drupal-8/core/lib/Drupal/Core/Entity/EntityTypeManager.php line 125
[09-Jul-2016 01:05:19 Australia/Sydney] PHP Fatal error:  Call to a member function id() on a non-object in /Applications/MAMP/htdocs/drupal-8/modules/metatag/src/Tests/MetatagFieldTest.php on line 113
agentrickard’s picture

I have looked for a half hour and have no idea why this test would fail. It can't find the "entity_test" entity type.

damienmckenna’s picture

Version: 8.x-1.0-beta9 » 8.x-1.x-dev
it-cru’s picture

With this patch enabled with beta9 we can't create taxonomy terms any more.

The last submitted patch, 3: metatag-2748615-empty-defaults-3.patch, failed testing.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 12: metatag-n2748615-12.patch, failed testing.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new5.13 KB

Ok, changing MetatagFieldTest so that every drupalPostForm() call is preceded by a drupalGet() to confirm the form loads in the first place.

Status: Needs review » Needs work

The last submitted patch, 14: metatag-n2748615-14.patch, failed testing.

damienmckenna’s picture

So 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.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new967 bytes
new5.23 KB

It'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 179

Status: Needs review » Needs work

The last submitted patch, 17: metatag-n2748615-17.patch, failed testing.

damienmckenna’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 17: metatag-n2748615-17.patch, failed testing.

damienmckenna’s picture

StatusFileSize
new217.11 KB

This doesn't fully resolve the problem anyway, because the form doesn't overwrite global values unless they're empty, e.g.:

The node/add/article form.

damienmckenna’s picture

This 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.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new5.3 KB

Skipping the $type variable if it's empty.

damienmckenna’s picture

damienmckenna’s picture

StatusFileSize
new1.15 KB

Just the changes to metatag.module.

damienmckenna’s picture

Status: Needs review » Fixed
damienmckenna’s picture

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

.. but this still needs more work and tests to confirm that the correct default values are being loaded.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new4.18 KB

These tests should pass, but they won't.

Status: Needs review » Needs work

The last submitted patch, 29: metatag-n2748615-29.patch, failed testing.

damienmckenna’s picture

This happens because of the usage of array_merge() to combine the various levels of defaults. So that needs to be fixed.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new7.11 KB

So 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?

Status: Needs review » Needs work

The last submitted patch, 32: metatag-n2748615-32.patch, failed testing.

damienmckenna’s picture

Ah! 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.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new7.5 KB
new524 bytes

Lets 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.

Status: Needs review » Needs work

The last submitted patch, 35: metatag-n2748615-35.patch, failed testing.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new7.76 KB
new382 bytes

Lets see if this works.

Status: Needs review » Needs work

The last submitted patch, 37: metatag-n2748615-37.patch, failed testing.

damienmckenna’s picture

StatusFileSize
new481 bytes
new7.77 KB

Somehow 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.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new8.36 KB
new611 bytes

So, maybe we just need to skip the unserialize() step if the string is empty?

damienmckenna’s picture

StatusFileSize
new2.18 KB
new9.18 KB

A slightly better approach - instead of erasing the entire default_value structure, just erase each individual '#default_value' value.

damienmckenna’s picture

People 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?

damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks all!

Status: Fixed » Closed (fixed)

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

drupalninja99’s picture

StatusFileSize
new60.81 KB

@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.

khaled.zaidan’s picture

Yeah, 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 :)

damienmckenna’s picture

Please test the latest -dev release, this was fixed in #2817309: Metatag shows predefined global values every time in forms (with tests!).