You are here

public function ManageContext::buildForm in Chaos Tool Suite (ctools) 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ManageContext.php, line 98

Class

ManageContext

Namespace

Drupal\ctools\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $cached_values = $form_state
    ->getTemporaryValue('wizard');
  $this->machine_name = $cached_values['id'];
  $form['items'] = [
    '#type' => 'markup',
    '#prefix' => '<div id="configured-contexts">',
    '#suffix' => '</div>',
    '#theme' => 'table',
    '#header' => [
      $this
        ->t('Context ID'),
      $this
        ->t('Label'),
      $this
        ->t('Data Type'),
      $this
        ->t('Options'),
    ],
    '#rows' => $this
      ->renderRows($cached_values),
    '#empty' => $this
      ->t('No contexts or relationships have been added.'),
  ];
  foreach ($this->typedDataManager
    ->getDefinitions() as $type => $definition) {
    $types[$type] = $definition['label'];
  }
  if (isset($types['entity'])) {
    unset($types['entity']);
  }
  asort($types);
  $form['context'] = [
    '#type' => 'select',
    '#options' => $types,
  ];
  $form['add'] = [
    '#type' => 'submit',
    '#name' => 'add',
    '#value' => $this
      ->t('Add new context'),
    '#ajax' => [
      'callback' => [
        $this,
        'addContext',
      ],
      'event' => 'click',
    ],
    '#submit' => [
      'callback' => [
        $this,
        'submitForm',
      ],
    ],
  ];
  $form['relationships'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add a relationship'),
    '#options' => $this
      ->getAvailableRelationships($cached_values),
    '#access' => $this->relationships,
  ];
  $form['add_relationship'] = [
    '#type' => 'submit',
    '#name' => 'add_relationship',
    '#value' => $this
      ->t('Add Relationship'),
    '#ajax' => [
      'callback' => [
        $this,
        'addRelationship',
      ],
      'event' => 'click',
    ],
    '#submit' => [
      'callback' => [
        $this,
        'submitForm',
      ],
    ],
    '#access' => $this->relationships,
  ];
  return $form;
}