You are here

public function SchemaActionTrait::actionForm in Schema.org Metatag 8

Create the form element.

Parameters

array $input_values: An array of values passed from a higher level form element to this.

Return value

array The form element.

1 call to SchemaActionTrait::actionForm()
SchemaActionBase::form in src/Plugin/metatag/Tag/SchemaActionBase.php
Generate a form element for this meta tag.

File

src/Plugin/metatag/Tag/SchemaActionTrait.php, line 39

Class

SchemaActionTrait
Schema.org Action trait.

Namespace

Drupal\schema_metatag\Plugin\metatag\Tag

Code

public function actionForm(array $input_values) {
  $all_types = static::getAllActions(TRUE);
  $all_options = array_combine($all_types, $all_types);
  $input_values += $this
    ->SchemaMetatagManager()
    ->defaultInputValues();
  $value = $input_values['value'];
  $form['#type'] = 'fieldset';
  $form['#title'] = $input_values['title'];
  $form['#description'] = $input_values['description'];
  $form['#tree'] = TRUE;

  // The assumption is that the list will be limited to specific actions
  // by the values passed in here. Missing information means display nothing.
  $actions = $input_values['actions'];
  if (empty($actions)) {
    return [];
  }

  // 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;

  // Add a pivot option to the form.
  $form['pivot'] = $this
    ->pivotForm($value);
  $form['pivot']['#states'] = $visibility;
  $options = [];
  foreach ($all_options as $type => $label) {
    if (in_array($type, $actions)) {
      $options[$type] = $all_options[$type];
    }
  }
  $form['@type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('@type'),
    '#default_value' => !empty($value['@type']) ? $value['@type'] : '',
    '#empty_option' => t('- None -'),
    '#empty_value' => '',
    '#options' => $options,
    '#required' => $input_values['#required'],
    '#weight' => -10,
  ];

  // Build the form one object type at a time, using the visibility settings
  // to hide/show only form elements for the selected type.
  foreach ($options as $type => $label) {

    // Properties specific to an action type appear only for that type.
    // Weight these properties ahead of general action properties.
    $parent_type = static::getActionType($type);
    $properties = static::actionProperties($parent_type);
    foreach ($properties as $key => $property) {
      if (empty($property['form'])) {
        $form[$key] = [
          '#type' => 'textfield',
          '#title' => $key,
          '#default_value' => !empty($value[$key]) ? $value[$key] : '',
          '#empty_option' => t('- None -'),
          '#empty_value' => '',
          '#required' => $input_values['#required'],
          '#description' => $property['description'],
          '#states' => $visibility,
          '#weight' => 0,
        ];
      }
      else {
        $sub_values = [
          'title' => $key,
          'description' => $property['description'],
          'value' => !empty($value[$key]) ? $value[$key] : [],
          '#required' => $input_values['#required'],
          'visibility_selector' => $input_values['visibility_selector'] . '[' . $key . ']',
        ];
        $method = $property['form'];
        $form[$key] = self::$method($sub_values);
        $form[$key]['#states'] = $visibility;
        $form[$key]['#weight'] = 0;
      }
    }
  }

  // Properties common to all actions appear for any action type.
  // Weight these after the action-specific properties.
  $properties = static::actionProperties('All');
  foreach ($properties as $key => $property) {
    if (empty($property['form'])) {
      $form[$key] = [
        '#type' => 'textfield',
        '#title' => $key,
        '#default_value' => !empty($value[$key]) ? $value[$key] : '',
        '#empty_option' => t('- None -'),
        '#empty_value' => '',
        '#required' => $input_values['#required'],
        '#description' => $property['description'],
        '#states' => $visibility,
        '#weight' => 5,
      ];
    }
    else {
      $sub_values = [
        'title' => $key,
        'description' => $property['description'],
        'value' => !empty($value[$key]) ? $value[$key] : [],
        '#required' => $input_values['#required'],
        'visibility_selector' => $input_values['visibility_selector'] . '[' . $key . ']',
      ];
      $method = $property['form'];
      $form[$key] = self::$method($sub_values);
      $form[$key]['#states'] = $visibility;
      $form[$key]['#weight'] = 5;
    }
  }
  return $form;
}