You are here

public function PropertyTypeBase::form in Schema.org Metatag 8.2

Create a complete form element for this property type.

Parameters

array $input_values: An array of values to be passed to the form creator, including:

  • @var string 'title' The title to use for the form element.
  • @var string 'description' The description to use for the form element.
  • @var array 'value' The current value of the form element.
  • @var string 'visibility_selector' The selector to use in assessing form element visibility, usually the @type element.
  • @var array 'tree_parent' The top level to use for @type, defaults to ''.
  • @var int 'tree_depth' The depth to go in the tree hierarchy, defaults to -1.
  • @var string 'multiple' Whether multiple values should be allowed, defaults to FALSE.

Return value

array Return a form array.

Overrides PropertyTypeInterface::form

1 method overrides PropertyTypeBase::form()
ItemListElement::form in src/Plugin/schema_metatag/PropertyType/ItemListElement.php
Create a complete form element for this property type.

File

src/Plugin/schema_metatag/PropertyTypeBase.php, line 170

Class

PropertyTypeBase
Base class for Property type plugins.

Namespace

Drupal\schema_metatag\Plugin\schema_metatag

Code

public function form(array $input_values) {
  $default_values = $this
    ->schemaMetatagManager()
    ->defaultInputValues();
  $input_values = array_merge($default_values, $input_values);

  // If no tree values were supplied, default to the values of the property
  // type plugin.
  if (empty($input_values['tree_parent'])) {
    $input_values['tree_parent'] = $this
      ->getTreeParent();
    $input_values['tree_depth'] = $this
      ->getTreeDepth();
  }

  // The properties and property types to generate for this form.
  $sub_properties = $this
    ->getSubProperties();
  if (!empty($sub_properties)) {
    $form['#type'] = 'fieldset';
    $form['#title'] = $input_values['title'];
    $form['#description'] = $input_values['description'];
    $form['#tree'] = TRUE;

    // For each sub property, generate a form element for the sub property
    // by invoking an instance of that child property type.
    foreach ($sub_properties as $sub_property_name => $values) {
      $sub_property_type = $values['id'];
      $child_property = $this
        ->getChildPropertyType($sub_property_type);
      $sub_property_value = is_array($input_values['value']) && array_key_exists($sub_property_name, $input_values['value']) ? $input_values['value'][$sub_property_name] : NULL;
      $sub_input_values['title'] = $values['label'];
      $sub_input_values['description'] = $values['description'];
      $sub_input_values['value'] = $sub_property_value;
      $sub_input_values['visibility_selector'] = $input_values['visibility_selector'];
      if (!empty($values['tree_parent'])) {
        $sub_input_values['visibility_selector'] .= "[{$sub_property_name}]";
      }

      // Pass parent tree values when empty, otherwise give each sub property
      // its own tree values.
      $sub_input_values['tree_parent'] = empty($values['tree_parent']) ? $input_values['tree_parent'] : $values['tree_parent'];
      $sub_input_values['tree_depth'] = empty($values['tree_depth']) ? $input_values['tree_depth'] : $values['tree_depth'];

      // Generate the sub property form element.
      $form[$sub_property_name] = $child_property
        ->form($sub_input_values);
      if ($sub_property_name != '@type') {

        // Add #states to hide this whole section if @type is empty.
        $form[$sub_property_name]['#states'] = $this
          ->getVisibility($input_values);
      }
      else {

        // Add a pivot element to the top of multiple value forms.
        if (!empty($input_values['multiple'])) {
          $value = is_array($input_values['value']) && array_key_exists('pivot', $input_values['value']) ? $input_values['value']['pivot'] : 0;
          $form['pivot'] = $this
            ->pivotForm($value);
          $form['pivot']['#states'] = $this
            ->getVisibility($input_values);
        }
      }
    }
  }
  else {
    $form = $this
      ->formElement($input_values);
  }
  $form['#element_validate'] = [
    [
      get_class($this),
      'validateProperty',
    ],
  ];
  return $form;
}