diff --git a/page_manager_ui/src/Form/PageContextsForm.php b/page_manager_ui/src/Form/PageContextsForm.php deleted file mode 100644 index 4116512..0000000 --- a/page_manager_ui/src/Form/PageContextsForm.php +++ /dev/null @@ -1,185 +0,0 @@ -getTemporaryValue('wizard'); - $this->machine_name = $cached_values['id']; - $form['items'] = [ - '#type' => 'markup', - '#prefix' => '
', - '#suffix' => '
', - '#theme' => 'table', - '#header' => [$this->t('Context'), $this->t('Type'), $this->t('Operations')], - '#rows' => $this->renderRows($cached_values), - '#empty' => $this->t('No Contexts configured for this variant.') - ]; - $types = []; - foreach (\Drupal::typedDataManager()->getDefinitions() as $type => $definition) { - $types[$type] = $definition['label']; - } - if (isset($types['entity'])) { - unset($types['entity']); - } - $form['types'] = [ - '#type' => 'select', - '#options' => $types, - ]; - $form['add'] = [ - '#type' => 'submit', - '#name' => 'add', - '#value' => $this->t('Add new context'), - '#ajax' => [ - 'callback' => [$this, 'add'], - 'event' => 'click', - ], - '#submit' => [ - 'callback' => [$this, 'submitForm'], - ] - ]; - return $form; - } - - public function add(array &$form, FormStateInterface $form_state) { - $type = $form_state->getValue('types'); - $content = \Drupal::formBuilder()->getForm('\Drupal\page_manager_ui\Form\StaticContextConfigure', 'add', $type, $this->getTempstoreId(), $this->machine_name); - $content['#attached']['library'][] = 'core/drupal.dialog.ajax'; - $cached_values = $form_state->getTemporaryValue('wizard'); - list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $type); - $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]); - $response = new AjaxResponse(); - $response->addCommand(new OpenModalDialogCommand($this->t('Add new context'), $content, array('width' => '700'))); - return $response; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $cached_values = $form_state->getTemporaryValue('wizard'); - - /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $add */ - $add = $form_state->getValue('add'); - if ($add->getUntranslatedString() == 'Add new context') { - list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('types')); - $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters); - } - } - - protected function renderRows($cached_values) { - $contexts = []; - /** @var $page \Drupal\page_manager\Entity\Page */ - $page = $cached_values['page']; - /** - * @var string $parameter - * @var \Drupal\Core\Plugin\Context\ContextInterface $context - */ - foreach ($page->getContexts() as $parameter => $context) { - // @todo this list should be replaced with some sort of context type check. - if (!in_array($parameter, ['current_user'])) { - list($route_partial, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $parameter); - $build = [ - '#type' => 'operations', - '#links' => $this->getOperations($route_partial, $route_parameters), - ]; - } - else { - $build = []; - } - - $contexts[$parameter] = [ - $context->getContextDefinition()->getLabel(), - $context->getContextDefinition()->getDataType(), - 'operations' => [ - 'data' => $build, - ], - ]; - } - return $contexts; - } - - protected function getOperations($route_name_base, array $route_parameters = array()) { - $operations['edit'] = array( - 'title' => t('Edit'), - 'url' => new Url($route_name_base . '.edit', $route_parameters), - 'weight' => 10, - 'attributes' => array( - 'class' => ['use-ajax'], - 'data-dialog-type' => 'modal', - 'data-dialog-options' => Json::encode([ - 'width' => 700, - ]), - ), - ); - $operations['delete'] = array( - 'title' => t('Delete'), - 'url' => new Url($route_name_base . '.delete', $route_parameters), - 'weight' => 100, - 'attributes' => array( - 'class' => array('use-ajax'), - 'data-dialog-type' => 'modal', - 'data-dialog-options' => Json::encode([ - 'width' => 700, - ]), - ), - ); - return $operations; - } - - /** - * Returns the tempstore id to use. - * - * @return string - */ - protected function getTempstoreId() { - return 'page_manager.page'; - } - - protected function getOperationsRouteInfo($cached_values, $machine_name, $row) { - return ['entity.page.context', ['machine_name' => $machine_name, 'data_type' => $row]]; - } - - /** - * The route to which condition 'add' actions should submit. - * - * @return string - */ - protected function getAddRoute($cached_values) { - return 'entity.page.context.add'; - } - -} diff --git a/page_manager_ui/src/Form/PageParametersForm.php b/page_manager_ui/src/Form/PageParametersForm.php new file mode 100644 index 0000000..2b6ff96 --- /dev/null +++ b/page_manager_ui/src/Form/PageParametersForm.php @@ -0,0 +1,129 @@ +getTemporaryValue('wizard'); + $this->machine_name = $cached_values['id']; + $form['items'] = [ + '#type' => 'markup', + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'table', + '#header' => [$this->t('Label'), $this->t('Type'), $this->t('Operations')], + '#rows' => $this->renderRows($cached_values), + '#empty' => $this->t('There are no parameters defined for this page.') + ]; + return $form; + } + + protected function renderRows($cached_values) { + $parameters = []; + /** @var $page \Drupal\page_manager\Entity\Page */ + $page = $cached_values['page']; + /** + * @var string $parameter + * @var \Drupal\Core\Plugin\Context\ContextInterface $context + */ + foreach ($page->getContexts() as $parameter => $context) { + // @todo this list should be replaced with some sort of context type check. + if (!in_array($parameter, ['current_user'])) { + list($route_partial, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $parameter); + $build = [ + '#type' => 'operations', + '#links' => $this->getOperations($route_partial, $route_parameters), + ]; + } + else { + $build = []; + } + + $contexts[$parameter] = [ + $context->getContextDefinition()->getLabel(), + $context->getContextDefinition()->getDataType(), + 'operations' => [ + 'data' => $build, + ], + ]; + } + return $contexts; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $cached_values = $form_state->getTemporaryValue('wizard'); + + /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $add */ + $add = $form_state->getValue('add'); + if ($add->getUntranslatedString() == 'Add new context') { + list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('types')); + $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters); + } + } + + protected function getOperations($route_name_base, array $route_parameters = array()) { + $operations['edit'] = array( + 'title' => t('Edit'), + 'url' => new Url($route_name_base . '.edit', $route_parameters), + 'weight' => 10, + 'attributes' => array( + 'class' => ['use-ajax'], + 'data-dialog-type' => 'modal', + 'data-dialog-options' => Json::encode([ + 'width' => 700, + ]), + ), + ); + return $operations; + } + + /** + * Returns the tempstore id to use. + * + * @return string + */ + protected function getTempstoreId() { + return 'page_manager.page'; + } + + protected function getOperationsRouteInfo($cached_values, $machine_name, $row) { + return ['entity.page.context', ['machine_name' => $machine_name, 'data_type' => $row]]; + } + +} diff --git a/page_manager_ui/src/Form/PageVariantContextsForm.php b/page_manager_ui/src/Form/PageVariantContextsForm.php new file mode 100644 index 0000000..aaa7821 --- /dev/null +++ b/page_manager_ui/src/Form/PageVariantContextsForm.php @@ -0,0 +1,186 @@ +getTemporaryValue('wizard'); + $this->machine_name = $cached_values['id']; + $form['items'] = [ + '#type' => 'markup', + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'table', + '#header' => [$this->t('Context'), $this->t('Type'), $this->t('Operations')], + '#rows' => $this->renderRows($cached_values), + '#empty' => $this->t('No Contexts configured for this variant.') + ]; + $types = []; + foreach (\Drupal::typedDataManager()->getDefinitions() as $type => $definition) { + $types[$type] = $definition['label']; + } + if (isset($types['entity'])) { + unset($types['entity']); + } + asort($types); + $form['types'] = [ + '#type' => 'select', + '#options' => $types, + ]; + $form['add'] = [ + '#type' => 'submit', + '#name' => 'add', + '#value' => $this->t('Add new context'), + '#ajax' => [ + 'callback' => [$this, 'add'], + 'event' => 'click', + ], + '#submit' => [ + 'callback' => [$this, 'submitForm'], + ] + ]; + return $form; + } + + public function add(array &$form, FormStateInterface $form_state) { + $type = $form_state->getValue('types'); + $content = \Drupal::formBuilder()->getForm('\Drupal\page_manager_ui\Form\StaticContextConfigure', 'add', $type, $this->getTempstoreId(), $this->machine_name); + $content['#attached']['library'][] = 'core/drupal.dialog.ajax'; + $cached_values = $form_state->getTemporaryValue('wizard'); + list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $type); + $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]); + $response = new AjaxResponse(); + $response->addCommand(new OpenModalDialogCommand($this->t('Add new context'), $content, array('width' => '700'))); + return $response; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $cached_values = $form_state->getTemporaryValue('wizard'); + + /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $add */ + $add = $form_state->getValue('add'); + if ($add->getUntranslatedString() == 'Add new context') { + list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('types')); + $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters); + } + } + + protected function renderRows($cached_values) { + $contexts = []; + /** @var $page \Drupal\page_manager\Entity\Page */ + $page_variant = $cached_values['page_variant']; + /** + * @var string $parameter + * @var \Drupal\Core\Plugin\Context\ContextInterface $context + */ + foreach ($page_variant->getContexts() as $parameter => $context) { + // @todo this list should be replaced with some sort of context type check. + if (!in_array($parameter, ['current_user'])) { + list($route_partial, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $parameter); + $build = [ + '#type' => 'operations', + '#links' => $this->getOperations($route_partial, $route_parameters), + ]; + } + else { + $build = []; + } + + $contexts[$parameter] = [ + $context->getContextDefinition()->getLabel(), + $context->getContextDefinition()->getDataType(), + 'operations' => [ + 'data' => $build, + ], + ]; + } + return $contexts; + } + + protected function getOperations($route_name_base, array $route_parameters = array()) { + $operations['edit'] = array( + 'title' => t('Edit'), + 'url' => new Url($route_name_base . '.edit', $route_parameters), + 'weight' => 10, + 'attributes' => array( + 'class' => ['use-ajax'], + 'data-dialog-type' => 'modal', + 'data-dialog-options' => Json::encode([ + 'width' => 700, + ]), + ), + ); + $operations['delete'] = array( + 'title' => t('Delete'), + 'url' => new Url($route_name_base . '.delete', $route_parameters), + 'weight' => 100, + 'attributes' => array( + 'class' => array('use-ajax'), + 'data-dialog-type' => 'modal', + 'data-dialog-options' => Json::encode([ + 'width' => 700, + ]), + ), + ); + return $operations; + } + + /** + * Returns the tempstore id to use. + * + * @return string + */ + protected function getTempstoreId() { + return 'page_manager.page'; + } + + protected function getOperationsRouteInfo($cached_values, $machine_name, $row) { + return ['entity.page.context', ['machine_name' => $machine_name, 'data_type' => $row]]; + } + + /** + * The route to which condition 'add' actions should submit. + * + * @return string + */ + protected function getAddRoute($cached_values) { + return 'entity.page.context.add'; + } + +} diff --git a/page_manager_ui/src/Form/StaticContextConfigure.php b/page_manager_ui/src/Form/StaticContextConfigure.php index c8ac19c..41f95bc 100644 --- a/page_manager_ui/src/Form/StaticContextConfigure.php +++ b/page_manager_ui/src/Form/StaticContextConfigure.php @@ -63,9 +63,9 @@ class StaticContextConfigure extends FormBase { $this->machine_name = $machine_name; $cached_values = $this->getTempstore(); /** @var \Drupal\page_manager\PageInterface $page */ - $page = $cached_values['page']; + $page_variant = $cached_values['page_variant']; if ($op == 'edit') { - $context = $page->getStaticContext($data_type); + $context = $page_variant->getStaticContext($data_type); $form['type'] = [ '#type' => 'value', '#value' => $context['type'], @@ -133,7 +133,7 @@ class StaticContextConfigure extends FormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $cache_values = $this->getTempstore(); /** @var \Drupal\page_manager\PageInterface $page */ - $page = $cache_values['page']; + $page_variant = $cache_values['page_variant']; $value = $form_state->getValue('value'); $type = $form_state->getValue('type'); if (substr($type, 0, 7) == 'entity:') { @@ -145,7 +145,7 @@ class StaticContextConfigure extends FormBase { 'type' => $form_state->getValue('type'), 'value' => $value, ]; - $page->setStaticContext($form_state->getValue('id'), $config); + $page_variant->setStaticContext($form_state->getValue('id'), $config); $this->setTempstore($cache_values); list($route_name, $route_parameters) = $this->getParentRouteInfo($cache_values); $form_state->setRedirect($route_name, $route_parameters); diff --git a/page_manager_ui/src/Wizard/PageEditWizard.php b/page_manager_ui/src/Wizard/PageEditWizard.php index ea3a969..e70128d 100644 --- a/page_manager_ui/src/Wizard/PageEditWizard.php +++ b/page_manager_ui/src/Wizard/PageEditWizard.php @@ -69,6 +69,10 @@ class PageEditWizard extends PageWizardBase { 'title' => $this->t('General'), 'form' => '\Drupal\page_manager_ui\Form\PageVariantConfigureForm', ]; + $operations['contexts'] = [ + 'title' => $this->t('Contexts'), + 'form' => '\Drupal\page_manager_ui\Form\PageVariantContextsForm', + ]; $operations['selection'] = [ 'title' => $this->t('Selection Criteria'), 'form' => '\Drupal\page_manager_ui\Form\PageVariantSelectionForm', diff --git a/page_manager_ui/src/Wizard/PageWizardBase.php b/page_manager_ui/src/Wizard/PageWizardBase.php index 911e7dc..a857640 100644 --- a/page_manager_ui/src/Wizard/PageWizardBase.php +++ b/page_manager_ui/src/Wizard/PageWizardBase.php @@ -61,9 +61,9 @@ class PageWizardBase extends EntityFormWizardBase { 'title' => $this->t('Page Access'), 'form' => '\Drupal\page_manager_ui\Form\PageAccessForm', ]; - $operations['contexts'] = [ - 'title' => $this->t('Configure Contexts'), - 'form' => '\Drupal\page_manager_ui\Form\PageContextsForm', + $operations['parameters'] = [ + 'title' => $this->t('Page Parameters'), + 'form' => '\Drupal\page_manager_ui\Form\PageParametersForm', ]; return $operations;