You are here

SchemaAnswerBase.php in Schema.org Metatag 7

File

src/SchemaAnswerBase.php
View source
<?php

/**
 * Schema.org Answer items should extend this class.
 */
class SchemaAnswerBase extends SchemaNameBase {
  use SchemaAnswerTrait;

  /**
   * {@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['value'] = $this
      ->answerForm($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',
      'text',
      'url',
      'upvoteCount',
      'dateCreated',
      'author',
    ];
    foreach ($keys as $key) {
      switch ($key) {
        case '@type':
          $items[$key] = 'Answer';
          break;
        case 'author':
          $items[$key] = SchemaPersonOrgBase::testValue();
          break;
        default:
          $items[$key] = parent::testDefaultValue(1, '');
          break;
      }
    }
    return $items;
  }

  /**
   * {@inheritdoc}
   */
  public static function processedTestValue($items) {
    foreach ($items as $key => $value) {
      switch ($key) {
        case 'author':
          $items[$key] = SchemaPersonOrgBase::processedTestValue($items[$key]);
          break;
      }
    }
    return $items;
  }

}

Classes

Namesort descending Description
SchemaAnswerBase Schema.org Answer items should extend this class.