You are here

trait SchemaSpeakableTrait in Schema.org Metatag 8

Schema.org Speakable trait.

Hierarchy

File

src/Plugin/metatag/Tag/SchemaSpeakableTrait.php, line 8

Namespace

Drupal\schema_metatag\Plugin\metatag\Tag
View source
trait SchemaSpeakableTrait {

  /**
   * Return the SchemaMetatagManager.
   *
   * @return \Drupal\schema_metatag\SchemaMetatagManager
   *   The Schema Metatag Manager service.
   */
  protected abstract function schemaMetatagManager();

  /**
   * The form element.
   */
  public function speakableForm($input_values) {
    $input_values += $this
      ->schemaMetatagManager()
      ->defaultInputValues();
    $value = $input_values['value'];

    // Get the id for the nested @type element.
    $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'],
    ];
    $form['#type'] = 'fieldset';
    $form['#title'] = $input_values['title'];
    $form['#description'] = $input_values['description'];
    $form['#tree'] = TRUE;
    $form['@type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Type'),
      '#default_value' => !empty($value['@type']) ? $value['@type'] : '',
      '#empty_option' => t('- None -'),
      '#empty_value' => '',
      '#options' => [
        'SpeakableSpecification' => $this
          ->t('SpeakableSpecification'),
      ],
      '#description' => $this
        ->t('Please provide either xpath or cssSelector, not both.'),
    ];
    $form['xpath'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('xpath'),
      '#default_value' => !empty($value['xpath']) ? $value['xpath'] : '',
      '#description' => $this
        ->t('Separate xpaths by comma, as in: @example', [
        '@example' => '/html/head/title, /html/head/meta[@name=\'description\']/@content',
      ]),
      '#states' => $visibility,
    ];
    $form['cssSelector'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('cssSelector'),
      '#default_value' => !empty($value['cssSelector']) ? $value['cssSelector'] : '',
      '#description' => $this
        ->t('Separate selectors by comma, as in @example', [
        '@example' => '#title, #summary',
      ]),
      '#states' => $visibility,
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchemaSpeakableTrait::schemaMetatagManager abstract protected function Return the SchemaMetatagManager.
SchemaSpeakableTrait::speakableForm public function The form element.