function ConditionConfigure::buildForm

Same name in other branches
  1. 4.0.x src/Form/ConditionConfigure.php \Drupal\ctools\Form\ConditionConfigure::buildForm()

Overrides FormInterface::buildForm

File

src/Form/ConditionConfigure.php, line 73

Class

ConditionConfigure
Base class for condition configur operations.

Namespace

Drupal\ctools\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $condition = NULL, $tempstore_id = NULL, $machine_name = NULL) {
    $this->tempstore_id = $tempstore_id;
    $this->machine_name = $machine_name;
    $cached_values = $this->tempstore
        ->get($this->tempstore_id)
        ->get($this->machine_name);
    if (is_numeric($condition) || Uuid::isValid($condition)) {
        $id = $condition;
        $condition = $this->getConditions($cached_values)[$id];
        $instance = $this->manager
            ->createInstance($condition['id'], $condition);
    }
    else {
        $instance = $this->manager
            ->createInstance($condition, []);
    }
    $form_state->setTemporaryValue('gathered_contexts', $this->getContexts($cached_values));
    
    /** @var \Drupal\Core\Condition\ConditionInterface $instance */
    $form = $instance->buildConfigurationForm($form, $form_state);
    if (isset($id)) {
        // Conditionally set this form element so that we can update or add.
        $form['id'] = [
            '#type' => 'value',
            '#value' => $id,
        ];
    }
    $form['instance'] = [
        '#type' => 'value',
        '#value' => $instance,
    ];
    $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save'),
        '#ajax' => [
            'callback' => [
                $this,
                'ajaxSave',
            ],
        ],
    ];
    return $form;
}