function ManageContext::addContext

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

Add a context.

Parameters

array $form: The Drupal Form.

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

Return value

\Drupal\Core\Ajax\AjaxResponse Form ajax repsonse.

File

src/Form/ManageContext.php, line 188

Class

ManageContext
Manage Context Form.

Namespace

Drupal\ctools\Form

Code

public function addContext(array &$form, FormStateInterface $form_state) {
    $context = $form_state->getValue('context');
    $cached_values = $form_state->getTemporaryValue('wizard');
    $content = $this->formBuilder
        ->getForm($this->getContextClass($cached_values), $context, $this->getTempstoreId(), $this->machine_name);
    $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    [
        ,
        $route_parameters,
    ] = $this->getContextOperationsRouteInfo($cached_values, $this->machine_name, $context);
    $route_name = $this->getContextAddRoute($cached_values);
    $route_options = [
        'query' => [
            FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
    ];
    $url = Url::fromRoute($route_name, $route_parameters, $route_options);
    $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url->toString();
    $response = new AjaxResponse();
    $response->addCommand(new OpenModalDialogCommand($this->t('Add new context'), $content, [
        'width' => '700',
    ]));
    return $response;
}