You are here

class SchemaNameBase in Schema.org Metatag 7

All Schema.org tags should extend this class.

Hierarchy

Expanded class hierarchy of SchemaNameBase

26 string references to 'SchemaNameBase'
SchemaActionTrait::actionProperties in src/SchemaActionTrait.php
Return an array of the unique properties for an action type.
SchemaCreativeWorkTrait::creativeWorkProperties in src/SchemaCreativeWorkTrait.php
Return an array of the unique properties for an object type.
SchemaHasPartTrait::hasPartProperties in src/SchemaHasPartTrait.php
Return an array of the unique properties for an object type.
schema_article_example_metatag_info in schema_article_example/schema_article_example.metatag.inc
Implements hook_metatag_info().
schema_article_metatag_info in schema_article/schema_article.metatag.inc
Implements hook_metatag_info().

... See full list

File

src/SchemaNameBase.php, line 6

View source
class SchemaNameBase extends DrupalTextMetaTag implements SchemaMetatagTestTagInterface {

  /**
   * The schemaMetatagManager service.
   *
   * @var \Drupal\schema_metatag\schemaMetatagManager
   */
  protected $schemaMetatagManager;

  /**
   * Constructor.
   */
  function __construct(array $info, array $data = NULL) {
    parent::__construct($info, $data);
    $this->schemaMetatagManager = new SchemaMetatagManager();
  }

  /**
   * Return the SchemaMetatagManager.
   *
   * @return \Drupal\schema_metatag\SchemaMetatagManager
   *   The Schema Metatag Manager service.
   */
  protected function schemaMetatagManager() {
    return $this->schemaMetatagManager;
  }

  /**
   * Wrappers to create D7 methods that match D8 format.
   *
   * To make it possible to re-use some D8 code.
   */

  /**
   * {@inheritdoc}
   */
  public function t($str, $args = []) {
    return t($str, $args);
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginId() {
    return $this->info['name'];
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->info['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function description() {
    return $this->info['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function multiple() {
    return !empty($this->info['multiple']);
  }

  /**
   * {@inheritdoc}
   */
  public function value() {
    return !empty($this->data['value']) ? $this->data['value'] : '';
  }

  /**
   * The #states visibility selector for this element.
   */
  protected function visibilitySelector() {
    return 'metatags[und][' . $this->info['name'] . '][value]';
  }

  /**
   * {@inheritdoc}
   */
  public function getForm(array $options = array()) {
    $form = parent::getForm($options);

    // Add a validation callback to serialize nested arrays.
    $form['value']['#element_validate'][] = 'schema_metatag_element_validate';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getElement(array $options = array()) {
    $this->options = $options;
    $value = $this
      ->schemaMetatagManager()
      ->unserialize($this
      ->value());

    // If this is a complex array of value, process the array.
    if (is_array($value)) {

      // Clean out empty values.
      $value = $this
        ->schemaMetatagManager()
        ->arrayTrim($value);
    }
    if (empty($value)) {
      return '';
    }
    elseif (is_array($value)) {

      // If the item is an array of values,
      // walk the array and process the values.
      array_walk_recursive($value, 'static::processItem');

      // Recursively pivot each branch of the array.
      $value = static::pivotItem($value);
    }
    else {
      $this
        ->processItem($value);
    }
    $parts = explode('.', $this->info['name']);
    $id = 'schema_metatag_' . $this->info['name'];
    $element = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'schema_metatag' => TRUE,
        'group' => $parts[0],
        'name' => $parts[1],
        'content' => static::outputValue($value),
      ],
    ];
    return array(
      '#attached' => array(
        'drupal_add_html_head' => array(
          array(
            $element,
            $id,
          ),
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function pivotItem($array) {

    // See if any nested items need to be pivoted.
    // If pivot is set to 0, it would have been removed as an empty value.
    if (array_key_exists('pivot', $array)) {
      unset($array['pivot']);
      $array = $this
        ->schemaMetatagManager()
        ->pivot($array);
    }
    foreach ($array as $key => &$value) {
      if (is_array($value)) {
        $value = static::pivotItem($value);
      }
    }
    return $array;
  }

  /**
   * Nested elements that cannot be exploded.
   *
   * @return array
   *   Array of keys that might contain commas, or otherwise cannot be exploded.
   */
  public static function neverExplode() {
    return [
      'streetAddress',
      'reviewBody',
      'recipeInstructions',
    ];
  }

  /**
   * Process an individual item.
   *
   * This is a copy of the original processing done by Metatag module,
   * but applied to every item on the array of values.
   */
  protected function processItem(&$value, $key = 0) {
    $explode = $key === 0 ? $this
      ->multiple() : !in_array($key, static::neverExplode());

    // $this->getValue() will process all subelements of our array
    // but not all of them need that processing.
    // Swap in the individual values/info as though they were the only
    // values, do the processing, then return to the original values.
    $backup_data = $this->data;
    $backup_info = $this->info;
    $this->data['value'] = $value;
    if (!empty($this->info['url'])) {
      $this->info['url'] = $this->info['url'] && in_array($key, [
        'url',
        'sameAs',
      ]);
    }
    if (!empty($this->info['image'])) {
      $this->info['image'] = $this->info['image'] && in_array($key, [
        'url',
      ]);
    }
    $value = $this
      ->getValue($this->options);
    if ($explode) {
      $value = $this
        ->schemaMetatagManager()
        ->explode($value);

      // Clean out any empty values that might have been added by explode().
      if (is_array($value)) {
        $value = array_filter($value);
      }
    }

    // Swap back in the original values.
    $this->data = $backup_data;
    $this->info = $backup_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function outputValue($input_value) {
    return $input_value;
  }

  /**
   * {@inheritdoc}
   */
  public static function testValue() {
    return static::testDefaultValue(2, ' ');
  }

  /**
   * {@inheritdoc}
   */
  public static function processedTestValue($items) {
    return $items;
  }

  /**
   * {@inheritdoc}
   */
  public static function processTestExplodeValue($items) {
    if (!is_array($items)) {
      $items = SchemaMetatagManager::explode($items);

      // Clean out any empty values that might have been added by explode().
      if (is_array($items)) {
        array_filter($items);
      }
    }
    return $items;
  }

  /**
   * {@inheritdoc}
   */
  public static function randomUrl() {
    return 'http://google.com/' . static::testDefaultValue(1, '');
  }

  /**
   * {@inheritdoc}
   */
  public static function testDefaultValue($count = NULL, $delimiter = NULL) {
    $items = [];
    $min = 1;
    $max = isset($count) ? $count : 2;
    $delimiter = isset($delimiter) ? $delimiter : ' ';
    for ($i = $min; $i <= $max; $i++) {
      $items[] = SchemaMetatagManager::randomMachineName();
    }
    return implode($delimiter, $items);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultMetaTag::$data protected property The values submitted for this tag.
DrupalDefaultMetaTag::$info protected property All of the basic information about this tag.
DrupalDefaultMetaTag::$weight protected property This item's weight; used for sorting the output.
DrupalDefaultMetaTag::convertUrlToAbsolute protected function Make sure a given URL is absolute.
DrupalDefaultMetaTag::getWeight public function Calculate the weight of this meta tag. Overrides DrupalMetaTagInterface::getWeight
DrupalDefaultMetaTag::maxlength protected function Identify the maximum length of which strings will be allowed.
DrupalDefaultMetaTag::textSummary public static function Copied from text.module with the following changes:. Overrides DrupalMetaTagInterface::textSummary
DrupalDefaultMetaTag::tidyValue protected function Remove unwanted formatting from a meta tag.
DrupalDefaultMetaTag::truncate protected function Shorten a string to a certain length using ::textSummary().
DrupalTextMetaTag::getValue public function Get the string value of this meta tag. Overrides DrupalDefaultMetaTag::getValue 1
SchemaNameBase::$schemaMetatagManager protected property The schemaMetatagManager service.
SchemaNameBase::description public function
SchemaNameBase::getElement public function Get the HTML tag for this meta tag. Overrides DrupalDefaultMetaTag::getElement 1
SchemaNameBase::getForm public function Build the form for this meta tag. Overrides DrupalTextMetaTag::getForm 32
SchemaNameBase::getPluginId public function
SchemaNameBase::label public function
SchemaNameBase::multiple public function
SchemaNameBase::neverExplode public static function Nested elements that cannot be exploded.
SchemaNameBase::outputValue public static function Transform input value to its display output. Overrides SchemaMetatagTestTagInterface::outputValue 2
SchemaNameBase::pivotItem public static function
SchemaNameBase::processedTestValue public static function Provide a test output value for the input value. Overrides SchemaMetatagTestTagInterface::processedTestValue 17
SchemaNameBase::processItem protected function Process an individual item.
SchemaNameBase::processTestExplodeValue public static function Explode a test value. Overrides SchemaMetatagTestTagInterface::processTestExplodeValue
SchemaNameBase::randomUrl public static function Random absolute url for testing. Overrides SchemaMetatagTestTagInterface::randomUrl
SchemaNameBase::schemaMetatagManager protected function Return the SchemaMetatagManager.
SchemaNameBase::t public function
SchemaNameBase::testDefaultValue public static function Provide a random test value. Overrides SchemaMetatagTestTagInterface::testDefaultValue
SchemaNameBase::testValue public static function Provide a test input value for the property that will validate. Overrides SchemaMetatagTestTagInterface::testValue 33
SchemaNameBase::value public function
SchemaNameBase::visibilitySelector protected function The #states visibility selector for this element.
SchemaNameBase::__construct function Constructor. Overrides DrupalDefaultMetaTag::__construct 1