From 5af9bf4469c6d93184d4f04110b5d042577fbf9c Mon Sep 17 00:00:00 2001 From: Chris Skene Date: Mon, 15 Apr 2013 15:41:23 +1000 Subject: [PATCH] Issue #1970362 by xtfer: Added Support all attributes of the meta element. --- metatag.inc | 183 ++++++++++++++++++++++++++++++++++++++ metatag.module | 45 +++++++--- metatag_dc/metatag_dc.metatag.inc | 73 ++++----------- metatag_dc/metatag_dc.module | 25 ------ 4 files changed, 233 insertions(+), 93 deletions(-) diff --git a/metatag.inc b/metatag.inc index 0466bea..b00588b 100644 --- a/metatag.inc +++ b/metatag.inc @@ -117,6 +117,189 @@ class DrupalTextMetaTag extends DrupalDefaultMetaTag { } /** + * Extended meta tag controller for tags supporting the full attribute set. + */ +class DrupalExtendedMetaTag extends DrupalTextMetaTag { + + /** + * Implements DrupalMetaTagInterface::getForm(). + */ + public function getForm(array $options = array()) { + + $options += array( + 'token types' => array(), + ); + + $form['item'] = array( + '#type' => 'fieldset', + '#title' => $this->info['label'], + '#tree' => TRUE, + '#description' => !empty($this->info['description']) ? $this->info['description'] : '', + '#attributes' => array('class' => array('metatag-item')), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['item']['value'] = isset($this->info['form']) ? $this->info['form'] : array(); + + $form['item']['value'] += array( + '#type' => 'textfield', + '#title' => t('Value'), + '#default_value' => isset($this->data['value']) ? $this->data['value'] : '', + '#element_validate' => array('token_element_validate'), + '#token_types' => $options['token types'], + '#maxlength' => 1024, + '#length' => '60', + ); + + $form['item']['scheme'] = isset($this->info['scheme']) ? $this->info['scheme'] : array(); + + $form['item']['scheme'] += array( + '#type' => 'textfield', + '#title' => t('Scheme'), + '#default_value' => isset($this->data['scheme']) ? $this->data['scheme'] : '', + '#element_validate' => array('token_element_validate'), + '#token_types' => $options['token types'], + '#maxlength' => 1024, + ); + + $form['item']['lang'] = isset($this->info['lang']) ? $this->info['lang'] : array(); + + $form['item']['lang'] += array( + '#type' => 'textfield', + '#title' => t('Language'), + '#description' => t("The lang attribute's value is a language code that identifies a natural language spoken, written, or otherwise used for the communication of information among people."), + '#default_value' => isset($this->data['scheme']) ? $this->data['scheme'] : '', + '#element_validate' => array('token_element_validate'), + '#token_types' => $options['token types'], + '#maxlength' => 1024, + ); + + $form['item']['dir'] = isset($this->info['dir']) ? $this->info['dir'] : array(); + + $form['item']['dir'] += array( + '#type' => 'textfield', + '#title' => t('Text direction'), + '#description' => t("Specifies the base direction of directionally neutral text. It also specifies the directionality of tables. Can be either ltr or rtl."), + '#default_value' => isset($this->data['scheme']) ? $this->data['scheme'] : '', + '#element_validate' => array('token_element_validate'), + '#token_types' => $options['token types'], + '#maxlength' => 1024, + ); + + return $form; + } + + /** + * Implements DrupalMetaTagInterface::getValue(). + */ + public function getValue(array $options = array()) { + $name = "metatag:" . $options["instance"] . ":" . $this->info["name"]; + + $options += array( + 'token data' => array(), + 'clear' => TRUE, + 'sanitize' => TRUE, + 'raw' => FALSE, + ); + + if (!isset($this->data['item']['value'])) { + return NULL; + } + + $value = metatag_translate($name, $this->data['item']['value']); + if (empty($options['raw'])) { + // Give other modules the opportunity to use hook_metatag_pattern_alter() + // to modify defined token patterns and values before replacement. + drupal_alter('metatag_pattern', $value, $options['token data']); + $value = token_replace($value, $options['token data'], $options); + } + $value = strip_tags(decode_entities($value)); + $value = trim($value); + + return $value; + } + + /** + * Return the item attributes for the extended tag. + * + * @param array $options + * (optional) An array of options. + * + * @return array + * An array of values. + */ + public function getAttributes(array $options = array()) { + + $options += array( + 'token data' => array(), + 'clear' => TRUE, + 'sanitize' => TRUE, + 'raw' => FALSE, + ); + + $values = $this->data['item']; + + unset($values['value']); + + if (isset($values['lang'])) { + $values['xml:lang'] = $values['lang']; + } + + foreach ($values as $key => $value) { + if (empty($options['raw'])) { + $value = token_replace($value, $options['token data'], $options); + } + $value = strip_tags(decode_entities($value)); + $values[$key] = trim($value); + } + + return $values; + } + + /** + * Implements DrupalMetaTagInterface::getElement(). + */ + public function getElement(array $options = array()) { + + $element = isset($this->info['element']) ? $this->info['element'] : array(); + + $value = $this->getValue($options); + if (strlen($value) === 0) { + return array(); + } + + $element += array( + '#theme' => 'metatag', + '#tag' => 'meta', + '#id' => 'metatag_' . $this->info['name'], + '#name' => $this->info['name'], + '#value' => $value, + '#attributes' => $this->getAttributes($options), + ); + + // Add header information if desired. + if (!empty($this->info['header'])) { + $element['#attached']['drupal_add_http_header'][] = array( + $this->info['header'], + $value, + ); + } + + return array( + '#attached' => array( + 'drupal_add_html_head' => array( + array( + $element, + $element['#id'], + ), + ), + ), + ); + } +} + + +/** * Link type meta tag controller. */ class DrupalLinkMetaTag extends DrupalTextMetaTag { diff --git a/metatag.module b/metatag.module index cc5d0e8..aec1703 100644 --- a/metatag.module +++ b/metatag.module @@ -1500,18 +1500,43 @@ function metatag_config_instance_info($instance = NULL) { */ function metatag_filter_values_from_defaults(array &$values, array $defaults = array()) { foreach ($values as $metatag => $data) { + $default = isset($data['default']) ? $data['default'] : (isset($defaults[$metatag]['value']) ? $defaults[$metatag]['value'] : NULL); - if (isset($default) && isset($data['value']) && $default === $data['value']) { - // Meta tag has a default, and it matches user-submitted value. - unset($values[$metatag]); - } - 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]); + + if (isset($data['value']) && is_array($data['value'])) { + foreach ($data['value'] as $property => $value) { + + if (isset($default[$property]) && isset($data['value'][$property]) && $default[$property] === $data['value'][$property]) { + // Meta tag has a default, and it matches user-submitted value. + unset($values[$metatag][$property]); + } + elseif (!isset($default[$property]) && (is_string($data['value'][$property]) && !drupal_strlen($data['value'][$property]) || (is_array($data['value'][$property]) && !array_filter($data['value'][$property])))) { + // Metatag does not have a default, and user did not submit a value. + unset($values[$metatag][$property]); + } + + if (isset($values[$metatag]['default'][$property])) { + // Unset the default hidden value. + unset($values[$metatag]['default'][$property]); + } + } } - if (isset($values[$metatag]['default'])) { - // Unset the default hidden value. - unset($values[$metatag]['default']); + else { + if (isset($data['value'])) { + if (!empty($default) && $default === $data['value']) { + // Meta tag has a default, and it matches user-submitted value. + unset($values[$metatag]); + } + 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]); + } + } + + if (isset($values[$metatag]['default'])) { + // Unset the default hidden value. + unset($values[$metatag]['default']); + } } } } diff --git a/metatag_dc/metatag_dc.metatag.inc b/metatag_dc/metatag_dc.metatag.inc index 7f8094d..e7c8403 100644 --- a/metatag_dc/metatag_dc.metatag.inc +++ b/metatag_dc/metatag_dc.metatag.inc @@ -70,143 +70,100 @@ function metatag_dc_metatag_info() { $info['tags']['dcterms.title'] = array( 'label' => t('Dublin Core Title'), 'description' => t('The name given to the resource.'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', 'element' => array( '#type' => 'term', - '#theme' => 'metatag_dc', ), ); $info['tags']['dcterms.creator'] = array( 'label' => t('Dublin Core Creator'), 'description' => t('An entity primarily responsible for making the resource. Examples of a Creator include a person, an organization, or a service. Typically, the name of a Creator should be used to indicate the entity.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.subject'] = array( 'label' => t('Dublin Core Subject'), 'description' => t('The topic of the resource. Typically, the subject will be represented using keywords, key phrases, or classification codes. Recommended best practice is to use a controlled vocabulary. To describe the spatial or temporal topic of the resource, use the Coverage element.'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.description'] = array( 'label' => t('Dublin Core Description'), 'description' => t('An account of the resource. Description may include but is not limited to: an abstract, a table of contents, a graphical representation, or a free-text account of the resource.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.publisher'] = array( 'label' => t('Dublin Core Publisher'), 'description' => t('An entity responsible for making the resource available. Examples of a Publisher include a person, an organization, or a service. Typically, the name of a Publisher should be used to indicate the entity.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.contributor'] = array( 'label' => t('Dublin Core Contributor'), 'description' => t('An entity responsible for making contributions to the resource. Examples of a Contributor include a person, an organization, or a service. Typically, the name of a Contributor should be used to indicate the entity.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.date'] = array( 'label' => t('Dublin Core Date'), 'description' => t('A point or period of time associated with an event in the lifecycle of the resource. Date may be used to express temporal information at any level of granularity. Recommended best practice is to use an encoding scheme, such as the W3CDTF profile of ISO 8601 [W3CDTF].'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.type'] = array( 'label' => t('Dublin Core Type'), 'description' => t('The nature or genre of the resource. Recommended best practice is to use a controlled vocabulary such as the DCMI Type Vocabulary [DCMITYPE]. To describe the file format, physical medium, or dimensions of the resource, use the Format element.'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', 'form' => array( '#type' => 'select', '#options' => _metatag_dc_dcmi_type_vocabulary_options(), '#empty_option' => t('- None -'), ), - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.format'] = array( 'label' => t('Dublin Core Format'), 'description' => t('The file format, physical medium, or dimensions of the resource. Examples of dimensions include size and duration. Recommended best practice is to use a controlled vocabulary such as the list of Internet Media Types [MIME].'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.identifier'] = array( 'label' => t('Dublin Core Identifier'), 'description' => t('An unambiguous reference to the resource within a given context. Recommended best practice is to identify the resource by means of a string conforming to a formal identification system.'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.source'] = array( 'label' => t('Dublin Core Source'), 'description' => t('A related resource from which the described resource is derived. The described resource may be derived from the related resource in whole or in part. Recommended best practice is to identify the related resource by means of a string conforming to a formal identification system.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.language'] = array( 'label' => t('Dublin Core Language'), 'description' => t('A language of the resource. Recommended best practice is to use a controlled vocabulary such as RFC 4646 [RFC4646].'), - 'class' => 'DrupalTextMetaTag', + 'class' => 'DrupalExtendedMetaTag', 'group' => 'dublin-core', - 'element' => array( - '#theme' => 'metatag_dc', - ), ); $info['tags']['dcterms.relation'] = array( 'label' => t('Dublin Core Relation'), 'description' => t('A related resource. Recommended best practice is to identify the related resource by means of a string conforming to a formal identification system.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.coverage'] = array( 'label' => t('Dublin Core Coverage'), 'description' => t('The spatial or temporal topic of the resource, the spatial applicability of the resource, or the jurisdiction under which the resource is relevant. Spatial topic and spatial applicability may be a named place or a location specified by its geographic coordinates. Temporal topic may be a named period, date, or date range. A jurisdiction may be a named administrative entity or a geographic place to which the resource applies. Recommended best practice is to use a controlled vocabulary such as the Thesaurus of Geographic Names [TGN]. Where appropriate, named places or time periods can be used in preference to numeric identifiers such as sets of coordinates or date ranges.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); $info['tags']['dcterms.rights'] = array( 'label' => t('Dublin Core Rights'), 'description' => t('Information about rights held in and over the resource. Typically, rights information includes a statement about various property rights associated with the resource, including intellectual property rights.'), 'group' => 'dublin-core', - 'class' => 'DrupalTextMetaTag', - 'element' => array( - '#theme' => 'metatag_dc', - ), + 'class' => 'DrupalExtendedMetaTag', ); return $info; diff --git a/metatag_dc/metatag_dc.module b/metatag_dc/metatag_dc.module index 8c15070..b41a573 100644 --- a/metatag_dc/metatag_dc.module +++ b/metatag_dc/metatag_dc.module @@ -12,28 +12,3 @@ function metatag_dc_ctools_plugin_api($owner, $api) { return array('version' => 1); } } - -/** - * Implements hook_theme(). - */ -function metatag_dc_theme() { - $info['metatag_dc'] = array( - 'render element' => 'element', - ); - - return $info; -} - -/** - * Theme callback for a Dublin Core meta tag. - */ -function theme_metatag_dc($variables) { - $element = &$variables['element']; - element_set_attributes($element, array( - '#name' => 'name', - '#schema' => 'schema', - '#value' => 'content') - ); - unset($element['#value']); - return theme('html_tag', $variables); -} -- 1.7.11.1