View source
<?php
namespace Drupal\page_manager_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ctools\Form\ManageContext;
class AddVariantContextsForm extends ManageContext {
protected $relationships = FALSE;
public function addContext(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_variant = $cached_values['page_variant'];
$context = $form_state
->getValue('context');
$content = $this->formBuilder
->getForm($this
->getContextClass($cached_values), $context, $this
->getTempstoreId(), $this->machine_name, $page_variant
->id());
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
list(, $route_parameters) = $this
->getContextOperationsRouteInfo($cached_values, $this->machine_name, $context);
$content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = Url::fromRoute($this
->getContextAddRoute($cached_values), $route_parameters, [
'query' => [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
])
->toString();
$response = new AjaxResponse();
$response
->addCommand(new OpenModalDialogCommand($this
->t('Add new context'), $content, [
'width' => '700',
]));
return $response;
}
public function getFormId() {
return 'page_manager_variant_context_form';
}
protected function getContextClass($cached_values) {
return AddVariantStaticContextConfigure::class;
}
protected function getRelationshipClass($cached_values) {
}
protected function getContextAddRoute($cached_values) {
return 'entity.page_variant.add_step_form.context.add';
}
protected function getRelationshipAddRoute($cached_values) {
return 'entity.page_variant.add_step_form.context.add';
}
protected function getTempstoreId() {
return 'page_manager.page_variant';
}
protected function getContexts($cached_values) {
$page_variant = $cached_values['page_variant'];
return $page_variant
->getContexts();
}
protected function getContextOperationsRouteInfo($cached_values, $machine_name, $row) {
$page_variant = $cached_values['page_variant'];
return [
'entity.page_variant.add_step_form.context',
[
'page' => $page_variant
->getPage()
->id(),
'machine_name' => $machine_name,
'context_id' => $row,
],
];
}
protected function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row) {
$page_variant = $cached_values['page_variant'];
return [
'entity.page_variant.add_step_form.context',
[
'page' => $page_variant
->getPage()
->id(),
'machine_name' => $machine_name,
'context_id' => $row,
],
];
}
protected function isEditableContext($cached_values, $row) {
$page_variant = $cached_values['page_variant'];
$page = $page_variant
->getPage();
return empty($page
->getContexts()[$row]) && !empty($page_variant
->getContexts()[$row]);
}
}