You are here

class PropertyTypeBase in Schema.org Metatag 8.2

Base class for Property type plugins.

Hierarchy

Expanded class hierarchy of PropertyTypeBase

42 files declare their use of PropertyTypeBase
Action.php in src/Plugin/schema_metatag/PropertyType/Action.php
AggregateRating.php in src/Plugin/schema_metatag/PropertyType/AggregateRating.php
Answer.php in src/Plugin/schema_metatag/PropertyType/Answer.php
Book.php in src/Plugin/schema_metatag/PropertyType/Book.php
Boolean.php in src/Plugin/schema_metatag/PropertyType/Boolean.php

... See full list

File

src/Plugin/schema_metatag/PropertyTypeBase.php, line 17

Namespace

Drupal\schema_metatag\Plugin\schema_metatag
View source
class PropertyTypeBase extends PluginBase implements PropertyTypeInterface, SchemaMetatagTestTagInterface, ContainerFactoryPluginInterface {
  use StringTranslationTrait;

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

  /**
   * The SchemaMetatagClient service.
   *
   * @var \Drupal\schema_metatag\SchemaMetatagClientInterface
   */
  protected $schemaMetatagClient;

  /**
   * The propertyTypeManager service.
   *
   * @var \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeManager
   */
  protected $propertyTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance
      ->setSchemaMetatagManager($container
      ->get('schema_metatag.schema_metatag_manager'));
    $instance
      ->setSchemaMetatagClient($container
      ->get('schema_metatag.schema_metatag_client'));
    $instance
      ->setPropertyTypeManager($container
      ->get('plugin.manager.schema_property_type'));
    return $instance;
  }

  /**
   * Sets schemaMetatagManager service.
   *
   * @param \Drupal\schema_metatag\SchemaMetatagManagerInterface $schemaMetatagManager
   *   The Schema Metatag Manager service.
   */
  public function setSchemaMetatagManager(SchemaMetatagManagerInterface $schemaMetatagManager) {
    $this->schemaMetatagManager = $schemaMetatagManager;
  }

  /**
   * Sets SchemaMetatagClient service.
   *
   * @param \Drupal\schema_metatag\SchemaMetatagClientInterface $schemaMetatagClient
   *   The Schema.org client.
   */
  public function setSchemaMetatagClient(SchemaMetatagClientInterface $schemaMetatagClient) {
    $this->schemaMetatagClient = $schemaMetatagClient;
  }

  /**
   * Sets PropertyTypeManager service.
   *
   * @param \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeManager $propertyTypeManager
   *   The property type manager.
   */
  public function setPropertyTypeManager(PropertyTypeManager $propertyTypeManager) {
    $this->propertyTypeManager = $propertyTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function schemaMetatagManager() {
    return $this->schemaMetatagManager;
  }

  /**
   * {@inheritdoc}
   */
  public function schemaMetatagClient() {
    return $this->schemaMetatagClient;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getTreeDepth() {
    return !empty($this->pluginDefinition['tree_depth']) ? $this->pluginDefinition['tree_depth'] : -1;
  }

  /**
   * {@inheritdoc}
   */
  public function getPropertyType() {
    return $this->pluginDefinition['property_type'];
  }

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

  /**
   * {@inheritdoc}
   */
  public function propertyInfo($property_type, $with_parents = TRUE) {
    $all_properties = $this
      ->schemaMetatagClient()
      ->getProperties();
    $properties = [];
    $parents = [
      $property_type,
    ];
    if ($with_parents) {
      $parents += $this
        ->schemaMetatagClient()
        ->getParents($property_type);
    }
    foreach ($parents as $type) {
      $properties = array_merge($properties, $all_properties[$type]);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function getTree($parent, $depth) {
    $tree = [];
    foreach ((array) $parent as $item) {
      $tree = array_merge($this->schemaMetatagClient
        ->getTree($item, $depth), $tree);
    }
    return $tree;
  }

  /**
   * {@inheritdoc}
   */
  public function getOptionList($parent, $depth) {
    $list = [];
    foreach ((array) $parent as $item) {
      $list = array_merge($this->schemaMetatagClient
        ->getOptionList($item, $depth), $list);
    }
    return $list;
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $input_values) {
    $default_values = $this
      ->schemaMetatagManager()
      ->defaultInputValues();
    $input_values = array_merge($default_values, $input_values);

    // If no tree values were supplied, default to the values of the property
    // type plugin.
    if (empty($input_values['tree_parent'])) {
      $input_values['tree_parent'] = $this
        ->getTreeParent();
      $input_values['tree_depth'] = $this
        ->getTreeDepth();
    }

    // The properties and property types to generate for this form.
    $sub_properties = $this
      ->getSubProperties();
    if (!empty($sub_properties)) {
      $form['#type'] = 'fieldset';
      $form['#title'] = $input_values['title'];
      $form['#description'] = $input_values['description'];
      $form['#tree'] = TRUE;

      // For each sub property, generate a form element for the sub property
      // by invoking an instance of that child property type.
      foreach ($sub_properties as $sub_property_name => $values) {
        $sub_property_type = $values['id'];
        $child_property = $this
          ->getChildPropertyType($sub_property_type);
        $sub_property_value = is_array($input_values['value']) && array_key_exists($sub_property_name, $input_values['value']) ? $input_values['value'][$sub_property_name] : NULL;
        $sub_input_values['title'] = $values['label'];
        $sub_input_values['description'] = $values['description'];
        $sub_input_values['value'] = $sub_property_value;
        $sub_input_values['visibility_selector'] = $input_values['visibility_selector'];
        if (!empty($values['tree_parent'])) {
          $sub_input_values['visibility_selector'] .= "[{$sub_property_name}]";
        }

        // Pass parent tree values when empty, otherwise give each sub property
        // its own tree values.
        $sub_input_values['tree_parent'] = empty($values['tree_parent']) ? $input_values['tree_parent'] : $values['tree_parent'];
        $sub_input_values['tree_depth'] = empty($values['tree_depth']) ? $input_values['tree_depth'] : $values['tree_depth'];

        // Generate the sub property form element.
        $form[$sub_property_name] = $child_property
          ->form($sub_input_values);
        if ($sub_property_name != '@type') {

          // Add #states to hide this whole section if @type is empty.
          $form[$sub_property_name]['#states'] = $this
            ->getVisibility($input_values);
        }
        else {

          // Add a pivot element to the top of multiple value forms.
          if (!empty($input_values['multiple'])) {
            $value = is_array($input_values['value']) && array_key_exists('pivot', $input_values['value']) ? $input_values['value']['pivot'] : 0;
            $form['pivot'] = $this
              ->pivotForm($value);
            $form['pivot']['#states'] = $this
              ->getVisibility($input_values);
          }
        }
      }
    }
    else {
      $form = $this
        ->formElement($input_values);
    }
    $form['#element_validate'] = [
      [
        get_class($this),
        'validateProperty',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(array $input_values) {
    $value = $input_values['value'];
    $form['#type'] = 'textfield';
    $form['#title'] = $input_values['title'];
    $form['#description'] = $input_values['description'];
    $form['#default_value'] = !empty($value) ? $value : '';
    $form['#maxlength'] = 255;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function pivotForm($value) {
    $form = [
      '#type' => 'select',
      '#title' => 'Pivot',
      '#default_value' => $value,
      '#empty_option' => ' - ' . $this
        ->t('None') . ' - ',
      '#empty_value' => '',
      '#options' => [
        1 => $this
          ->t('Pivot'),
      ],
      '#description' => $this
        ->t('Combine and pivot multiple values to display them as multiple objects.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getVisibility(array $input_values) {
    $selector = ':input[name="' . $input_values['visibility_selector'] . '[@type]"]';
    $visibility = [
      'invisible' => [
        $selector => [
          'value' => '',
        ],
      ],
    ];
    $selector2 = $this
      ->schemaMetatagManager()
      ->altSelector($selector);
    $visibility2 = [
      'invisible' => [
        $selector2 => [
          'value' => '',
        ],
      ],
    ];
    $visibility['invisible'] = [
      $visibility['invisible'],
      $visibility2['invisible'],
    ];
    return $visibility;
  }

  /**
   * {@inheritdoc}
   */
  public static function validateProperty(array &$element, FormStateInterface $form_state) {

    // Extend as needed to validate property types.
  }

  /**
   * {@inheritdoc}
   */
  public function getChildPropertyType($plugin_id) {
    return $this->propertyTypeManager
      ->createInstance($plugin_id);
  }

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

  /**
   * {@inheritdoc}
   */
  public function testValue($original_type = '') {
    if (empty($this
      ->getSubProperties())) {
      return self::testDefaultValue(2, ' ');
    }
    else {
      $items = [];
      foreach ($this
        ->getSubProperties() as $property_name => $values) {
        $plugin = $this
          ->getChildPropertyType($values['id']);
        if ($property_name == '@type') {
          $items[$property_name] = $plugin
            ->testValue($original_type);
        }
        else {
          $type = !empty($values['tree_parent']) ? $values['tree_parent'] : $plugin
            ->getTreeParent();
          $test_type = is_array($type) ? array_shift($type) : $type;
          $items[$property_name] = $plugin
            ->testValue($test_type);
        }
      }
      return $items;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function processedTestValue($items) {
    if (empty($this
      ->getSubProperties())) {
      return $this
        ->processTestExplodeValue($items);
    }
    else {
      foreach ($this
        ->getSubProperties() as $property_name => $values) {
        $items[$property_name] = $this
          ->getChildPropertyType($values['id'])
          ->processedTestValue($items[$property_name]);
      }
    }
    return $items;
  }

  /**
   * {@inheritdoc}
   */
  public function processTestExplodeValue($items) {
    if (!is_array($items)) {
      $items = $this
        ->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 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[] = $this
        ->schemaMetatagManager()
        ->randomMachineName();
    }
    return implode($delimiter, $items);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
PropertyTypeBase::$propertyTypeManager protected property The propertyTypeManager service.
PropertyTypeBase::$schemaMetatagClient protected property The SchemaMetatagClient service.
PropertyTypeBase::$schemaMetatagManager protected property The schemaMetatagManager service.
PropertyTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PropertyTypeBase::form public function Create a complete form element for this property type. Overrides PropertyTypeInterface::form 1
PropertyTypeBase::formElement public function A property form element. Overrides PropertyTypeInterface::formElement 5
PropertyTypeBase::getChildPropertyType public function Get an instance of a child property type. Overrides PropertyTypeInterface::getChildPropertyType
PropertyTypeBase::getOptionList public function Create an option list for a given tree section. Overrides PropertyTypeInterface::getOptionList
PropertyTypeBase::getPropertyType public function The property type. Overrides PropertyTypeInterface::getPropertyType
PropertyTypeBase::getSubProperties public function The sub-properties. Overrides PropertyTypeInterface::getSubProperties
PropertyTypeBase::getTree public function Get some or all of the object tree as options for @type. Overrides PropertyTypeInterface::getTree
PropertyTypeBase::getTreeDepth public function The depth of the class tree to use for @type options. Overrides PropertyTypeInterface::getTreeDepth
PropertyTypeBase::getTreeParent public function The classes to use for the @type options of this property. Overrides PropertyTypeInterface::getTreeParent
PropertyTypeBase::getVisibility public function Construct the visibility selector for a set of values. Overrides PropertyTypeInterface::getVisibility
PropertyTypeBase::outputValue public function Transform input value to its display output. Overrides PropertyTypeInterface::outputValue 2
PropertyTypeBase::pivotForm public function Pivot form element. Overrides PropertyTypeInterface::pivotForm
PropertyTypeBase::processedTestValue public function Provide a test output value for the input value. Overrides SchemaMetatagTestTagInterface::processedTestValue
PropertyTypeBase::processTestExplodeValue public function Explode a test value. Overrides SchemaMetatagTestTagInterface::processTestExplodeValue
PropertyTypeBase::propertyInfo public function Get all the properties of a property type. Overrides PropertyTypeInterface::propertyInfo
PropertyTypeBase::schemaMetatagClient public function The Schema Metatag Client service. Overrides PropertyTypeInterface::schemaMetatagClient
PropertyTypeBase::schemaMetatagManager public function The Schema Metatag Manager service. Overrides PropertyTypeInterface::schemaMetatagManager
PropertyTypeBase::setPropertyTypeManager public function Sets PropertyTypeManager service.
PropertyTypeBase::setSchemaMetatagClient public function Sets SchemaMetatagClient service.
PropertyTypeBase::setSchemaMetatagManager public function Sets schemaMetatagManager service.
PropertyTypeBase::testDefaultValue public function Provide a random test value. Overrides SchemaMetatagTestTagInterface::testDefaultValue
PropertyTypeBase::testValue public function Provide a test input value for the property that will validate. Overrides SchemaMetatagTestTagInterface::testValue 3
PropertyTypeBase::validateProperty public static function Validates the property form when submitted. Overrides PropertyTypeInterface::validateProperty
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.