You are here

SchemaReviewBase.php in Schema.org Metatag 8

File

src/Plugin/metatag/Tag/SchemaReviewBase.php
View source
<?php

namespace Drupal\schema_metatag\Plugin\metatag\Tag;


/**
 * Provides a plugin to extend for the 'Review' meta tag.
 */
class SchemaReviewBase extends SchemaNameBase {
  use SchemaReviewTrait;

  /**
   * {@inheritdoc}
   */
  public function form(array $element = []) {
    $value = $this
      ->schemaMetatagManager()
      ->unserialize($this
      ->value());
    $input_values = [
      'title' => $this
        ->label(),
      'description' => $this
        ->description(),
      'value' => $value,
      '#required' => isset($element['#required']) ? $element['#required'] : FALSE,
      'visibility_selector' => $this
        ->visibilitySelector(),
    ];
    $form = parent::form($element);
    $form = $this
      ->reviewForm($input_values);
    if (empty($this
      ->multiple())) {
      unset($form['pivot']);
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public static function testValue() {
    $items = [];
    $keys = [
      '@type',
      'reviewBody',
      'datePublished',
      'author',
      'reviewRating',
    ];
    foreach ($keys as $key) {
      switch ($key) {
        case '@type':
          $items[$key] = 'Review';
          break;
        case 'author':
          $items[$key] = [
            '@type' => 'Person',
            'name' => parent::testDefaultValue(2, ' '),
          ];
          break;
        case 'reviewRating':
          $items[$key] = [
            '@type' => 'Rating',
            'ratingValue' => parent::testDefaultValue(2, ' '),
          ];
          break;
        default:
          $items[$key] = parent::testDefaultValue(2, ' ');
          break;
      }
    }
    return $items;
  }

}

Classes

Namesort descending Description
SchemaReviewBase Provides a plugin to extend for the 'Review' meta tag.