You are here

SchemaRatingBase.php in Schema.org Metatag 7

File

src/SchemaRatingBase.php
View source
<?php

/**
 * Provides a plugin to extend for the 'Rating' meta tag.
 */
class SchemaRatingBase extends SchemaNameBase {
  use SchemaRatingTrait;

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

    // Validation from parent::getForm() got wiped out, so add callback.
    $form['value']['#element_validate'][] = 'schema_metatag_element_validate';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public static function testValue() {
    $items = [];
    $keys = [
      '@type',
      'ratingValue',
      'bestRating',
      'worstRating',
      'ratingCount',
    ];
    foreach ($keys as $key) {
      switch ($key) {
        case '@type':
          $items[$key] = 'Rating';
          break;
        default:
          $items[$key] = parent::testDefaultValue(2, ' ');
          break;
      }
    }
    return $items;
  }

}

Classes

Namesort descending Description
SchemaRatingBase Provides a plugin to extend for the 'Rating' meta tag.