diff --git a/metatag.inc b/metatag.inc index 09bef4e..6b38e5a 100644 --- a/metatag.inc +++ b/metatag.inc @@ -69,7 +69,15 @@ class DrupalDefaultMetaTag implements DrupalMetaTagInterface { * A standard FormAPI array. */ public function getForm(array $options = array()) { - return array(); + $form = array(); + $form['allow_override'] = array( + '#type' => 'checkbox', + '#title' => t('Allow override'), + '#description' => t('Allow the parent configuration to override this one if the value is empty.'), + '#default_value' => isset($this->data['allow_override']) ? $this->data['allow_override'] : 0, + '#weight' => 20, + ); + return $form; } /** @@ -230,6 +238,7 @@ class DrupalTextMetaTag extends DrupalDefaultMetaTag { * {@inheritdoc} */ public function getForm(array $options = array()) { + $form = parent::getForm($options); $options += array( 'token types' => array(), ); @@ -448,6 +457,7 @@ class DrupalListMetaTag extends DrupalDefaultMetaTag { * {@inheritdoc} */ public function getForm(array $options = array()) { + $form = parent::getForm($options); $form['value'] = isset($this->info['form']) ? $this->info['form'] : array(); $form['value'] += array( @@ -488,6 +498,7 @@ class DrupalDateIntervalMetaTag extends DrupalDefaultMetaTag { * {@inheritdoc} */ public function getForm(array $options = array()) { + $form = parent::getForm($options); $form['value'] = array( '#type' => 'textfield', '#title' => t('!title interval', array('!title' => $this->info['label'])), diff --git a/metatag.module b/metatag.module index 5fbe3ea..7a2cea4 100644 --- a/metatag.module +++ b/metatag.module @@ -1257,16 +1257,30 @@ function metatag_metatags_view($instance, array $metatags = array(), array $opti // Don't output meta tags that only contain the pager. $current_pager = metatag_get_current_pager(); - + $overrides = array(); foreach ($metatags as $metatag => $data) { if ((!empty($data['value']) || (isset($data['value']) && is_numeric($data['value']))) && $metatag_instance = metatag_get_instance($metatag, $data)) { - $tag_output = $metatag_instance->getElement($options); - // Don't output the pager if that's all there is. - if ($tag_output != $current_pager) { - $output[$metatag] = $tag_output; + $output[$metatag] = $metatag_instance->getElement($options); + // If overrides are allowed for this metatag and the metatag is empty, + // flag that we need to apply overrides. + if (empty($output[$metatag]) && !empty($data['allow_override'])) { + $overrides[] = $metatag; } } + // If overrides are allowed for this metatag, flag that we need to apply + // overrides. + elseif (!empty($data['allow_override'])) { + $overrides[] = $metatag; + } + } + + // If there are any metatags that require overriding, do so. + if ($overrides) { + $instances = metatag_config_get_parent_instances($instance, TRUE, FALSE); + if ($instances) { + metatag_metatags_apply_overrides($output, $instances, $overrides, $options); + } } // Allow the output meta tags to be modified using @@ -1293,6 +1307,48 @@ function metatag_get_current_pager() { } } +/* + * Handle applying parent instance overrides to metatags. + * + * @param array $output + * The metatag output array. + * @param array $instances + * The parent instances to get overriding metatags from. + * @param array $overrides + * The metatag names that are to be overridden. + * @param array $options + * The options to use to generate the metatags. + */ +function metatag_metatags_apply_overrides(&$output, $instances, $overrides, $options) { + // Get the instance we are currently interested in. + $instance = array_shift($instances); + + // Get metatags for this instance. + $metatags = metatag_config_load_with_defaults($instance); + + foreach ($overrides as $key => $metatag) { + $data = $metatags[$metatag]; + if ((!empty($data['value']) || (isset($data['value']) && is_numeric($data['value']))) + && $metatag_instance = metatag_get_instance($metatag, $data)) { + $output[$metatag] = $metatag_instance->getElement($options); + // If we have a value or overrides aren't allowed at this level, remove + // them from the list of overrides. + if (!empty($output[$metatag]) || empty($data['allow_override'])) { + unset($overrides[$key]); + } + } + // If overrides aren't allowed at this level, remove them from the list of + // overrides. + elseif (empty($data['allow_override'])) { + unset($overrides[$key]); + } + } + + if ($overrides && $instances) { + metatag_metatags_apply_overrides($output, $instances, $overrides, $options); + } +} + /** * Returns metatags values. */ @@ -1452,6 +1508,12 @@ function metatag_metatags_form(array &$form, $instance, array $metatags = array( $metatag_form['#access'] = $form['metatags']['#access']; } + // Wrap the form in a fieldset as it will likely contain multiple fields. + $metatag_form = array( + '#type' => 'fieldset', + '#title' => isset($metatag_form['value']['#title']) ? $metatag_form['value']['#title'] : '', + ) + $metatag_form; + if (!empty($metatag_info['group'])) { $group_key = $metatag_info['group']; if (isset($info['groups'][$group_key]['label']) && !isset($form['metatags'][$langcode][$group_key])) { @@ -2290,12 +2352,16 @@ function metatag_filter_values_from_defaults(array &$values, array $defaults = a * * @param string $instance * A meta tag configuration instance. + * @param array $include_global + * Whether or not to include the global instance in the parents array. + * @param array $include_instance + * Whether or not to include the given instance in the parents array. * * @return array * An array of instances starting with the $instance parameter, with the end * of the array being the global instance. */ -function metatag_config_get_parent_instances($instance, $include_global = TRUE) { +function metatag_config_get_parent_instances($instance, $include_global = TRUE, $include_instance = TRUE) { $parents = array(); $segments = explode(':', $instance); while (count($segments) > 0) { @@ -2305,6 +2371,9 @@ function metatag_config_get_parent_instances($instance, $include_global = TRUE) if ($include_global && end($parents) !== 'global') { $parents[] = 'global'; } + if (!$include_instance) { + array_shift($parents); + } reset($parents); return $parents; }