function ManageContext::buildForm

Same name in other branches
  1. 8.x-3.x src/Form/ManageContext.php \Drupal\ctools\Form\ManageContext::buildForm()

Overrides FormInterface::buildForm

File

src/Form/ManageContext.php, line 100

Class

ManageContext
Manage Context Form.

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'] . " ({$type})";
        if ($definition['id'] === 'entity_revision') {
            $types[$type] .= ' (' . $this->t('Revision') . ')';
        }
    }
    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;
}