I've a content type with some default value set, if the field Head title isn't filled on the node form. Pretty basic stuff, the node title plus site name [node:title] | [site:name], which is actually the default module configuration.

When viewing the node page, the <title></title> is empty. I've tracked this down to:

class DrupalTitleMetaTag extends DrupalTextMetaTag {

  public function getElement(array $options = array()) {
    xdebug_break();
    $element = array();
    $value = check_plain($this->getValue($options)); // Empty value returned, so default should apply no?
    $element['#attached']['metatag_set_preprocess_variable'][] = array('html', 'head_title', $value);
    $element['#attached']['metatag_set_preprocess_variable'][] = array('html', 'head_array', array('title' => $value));
    return $element;
  }
}

Of course, if I set a title on the node form meta tag vertical tab, it is working.

Any suggestion on why this could not work?

Thank you!

Comments

jchatard’s picture

Title: DrupalTitleMetaTag doesn't return the default value » MetaTag doesn't return the default value when field is empty
Category: bug » support

Ok, I've found the reason why my default Title tags (and some others) didn't appeared. My input fields, on node form were empty. So not matching the default pattern, like [node:title] for example.

So, I have two solutions, manually edit each concerned node, and reset all meta tags fields to their default configuration, which seems to be how the module works.

Or edit the module to consider that an empty field is like a default pattern.

But I'd like to have some info from the maintainer of the module, to know if this makes sense or not, as I can easily understand that an enpty value is not a default value.

For information, here's the patch that I applied to consider that an empty field is like requesting the default value.

diff --git a/sites/all/modules/contrib/metatag/metatag.module b/sites/all/modules/contrib/metatag/metatag.module
index b99b868..00d4574 100644
--- a/sites/all/modules/contrib/metatag/metatag.module
+++ b/sites/all/modules/contrib/metatag/metatag.module
@@ -1435,6 +1435,9 @@ function metatag_filter_values_from_defaults(array &$values, array $defaults = a
     elseif (!isset($default) && (is_string($data['value']) && !drupal_strlen($data['value']) || (is_array($data['value']) && !array_filter($data['value'])))) {
       // Metatag does not have a default, and user did not submit a value.
       unset($values[$metatag]);
+    } elseif (drupal_strlen($data['value']) === 0 && isset($default)) {
+      // The user did not submit a value, but a default can be used.
+      unset($values[$metatag]);
     }
     if (isset($values[$metatag]['default'])) {
       // Unset the default hidden value.

Thanks,
Jérémy

damienmckenna’s picture

Status: Active » Closed (duplicate)

I think expanding #1934492: Bulk operation for reverting meta tags to allow specific meta tags to be erased (rather than all) would be the best approach.