You are here

public function SchemaHasPartTrait::hasPartForm in Schema.org Metatag 7

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 SchemaHasPartTrait::hasPartForm()
SchemaHasPartBase::getForm in src/SchemaHasPartBase.php
Build the form for this meta tag.

File

src/SchemaHasPartTrait.php, line 29

Class

SchemaHasPartTrait
Schema.org HasPart trait.

Code

public function hasPartForm(array $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;

  // Add a pivot option to the form.
  $form['pivot'] = $this
    ->pivotForm($value);
  $form['pivot']['#states'] = $visibility;
  $types = static::hasPartObjects();
  $options = array_combine($types, $types);
  $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 ($types as $type) {

    // Properties common to all objects appear for any object type.
    $properties = static::hasPartProperties('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,
        ];
      }
      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 . ']',
          'actions' => !empty($property['actions']) ? $property['actions'] : [],
        ];
        $method = $property['form'];
        $form[$key] = $this
          ->{$method}($sub_values);
        $form[$key]['#states'] = $visibility;
      }
    }

    // Properties specific to an object type appear only for that type.
    $properties = static::hasPartProperties($type);
    foreach ($properties as $key => $property) {
      $property_visibility = [
        'visible' => [
          $selector => [
            'value' => $type,
          ],
        ],
      ];
      $property_visibility2 = [
        'visible' => [
          $selector2 => [
            'value' => $type,
          ],
        ],
      ];
      $property_visibility['visible'] = [
        $property_visibility['visible'],
        $property_visibility2['visible'],
      ];
      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' => $property_visibility,
        ];
      }
      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 . ']',
          'visibility_type' => '@type',
          'actions' => !empty($property['actions']) ? $property['actions'] : [],
        ];
        $method = $property['form'];
        $form[$key] = $this
          ->{$method}($sub_values);
        $form[$key]['#states'] = $property_visibility;
      }
    }
  }
  return $form;
}