diff --git a/config/schema/page_manager.schema.yml b/config/schema/page_manager.schema.yml index 033b0c2..0cc98c5 100644 --- a/config/schema/page_manager.schema.yml +++ b/config/schema/page_manager.schema.yml @@ -36,9 +36,6 @@ page_manager.page.*: - type: mapping label: 'Static context' mapping: - machine_name: - type: string - label: 'Machine-readable name of the context' label: type: label label: 'Label of the context' diff --git a/page_manager.routing.yml b/page_manager.routing.yml index 10f63c6..62a608c 100644 --- a/page_manager.routing.yml +++ b/page_manager.routing.yml @@ -1,5 +1,3 @@ -#### Pages - page_manager.page_list: path: '/admin/structure/page_manager' defaults: @@ -48,8 +46,6 @@ entity.page.disable: requirements: _entity_access: 'page.update' -#### Access Conditions - page_manager.access_condition_select: path: '/admin/structure/page_manager/manage/{page}/access/select' defaults: @@ -82,8 +78,6 @@ page_manager.access_condition_delete: requirements: _entity_access: page.update -#### Static Contexts - page_manager.static_context_add: path: '/admin/structure/page_manager/manage/{page}/context/add' defaults: @@ -108,8 +102,6 @@ page_manager.static_context_delete: requirements: _entity_access: page.update -#### Display variants - page_manager.display_variant_select: path: '/admin/structure/page_manager/manage/{page}/add' defaults: @@ -174,8 +166,6 @@ page_manager.display_variant_delete_block: requirements: _entity_access: page.update -#### Selection Conditions - page_manager.selection_condition_select: path: '/admin/structure/page_manager/manage/{page}/manage/{display_variant_id}/selection/select' defaults: diff --git a/src/Entity/Page.php b/src/Entity/Page.php index 2a23b42..8118894 100644 --- a/src/Entity/Page.php +++ b/src/Entity/Page.php @@ -304,35 +304,17 @@ class Page extends ConfigEntityBase implements PageInterface { /** * {@inheritdoc} */ - public function addStaticContext(array $configuration) { - $static_contexts = $this->getStaticContexts(); - $static_contexts[] = $configuration; - $this->set('static_context', $static_contexts); - } - - /** - * {@inheritdoc} - */ public function getStaticContext($name) { - $static_contexts = $this->getStaticContexts(); - foreach ($static_contexts as $static_context) { - if ($static_context['machine_name'] == $name) { - return $static_context; - } + if (isset($this->static_context[$name])) { + return $this->static_context[$name]; } } /** * {@inheritdoc} */ - public function updateStaticContext($name, $configuration) { - $static_contexts = $this->getStaticContexts(); - foreach ($static_contexts as $key => $static_context) { - if ($static_context['machine_name'] == $name) { - $static_contexts[$key] = $configuration; - } - } - $this->set('static_context', $static_contexts); + public function setStaticContext($name, $configuration) { + $this->static_context[$name] = $configuration; return $this; } @@ -341,11 +323,7 @@ class Page extends ConfigEntityBase implements PageInterface { */ public function removeStaticContext($name) { $static_contexts = $this->getStaticContexts(); - foreach ($static_contexts as $key => $static_context) { - if ($static_context['machine_name'] == $name) { - unset($static_contexts[$key]); - } - } + unset($static_contexts[$name]); $this->set('static_context', $static_contexts); return $this; } diff --git a/src/EventSubscriber/StaticContext.php b/src/EventSubscriber/StaticContext.php index 616e7bf..1fe48bc 100644 --- a/src/EventSubscriber/StaticContext.php +++ b/src/EventSubscriber/StaticContext.php @@ -34,9 +34,9 @@ class StaticContext implements EventSubscriberInterface { $executable = $event->getPageExecutable(); $static_contexts = $executable->getPage()->get('static_context'); - foreach ($static_contexts as $static_context) { + foreach ($static_contexts as $name => $static_context) { $context = new EntityLazyLoadContext(new ContextDefinition($static_context['type'], $static_context['label']), $static_context['value']); - $executable->addContext($static_context['machine_name'], $context); + $executable->addContext($name, $context); } } diff --git a/src/Form/PageEditForm.php b/src/Form/PageEditForm.php index 8d35b2a..260ff1a 100644 --- a/src/Form/PageEditForm.php +++ b/src/Form/PageEditForm.php @@ -307,24 +307,4 @@ class PageEditForm extends PageFormBase { $form_state->setRedirect('page_manager.page_list'); } - /** - * Form submit callback to add a context reference. - */ - public function submitStaticContext(array $form, FormStateInterface $form_state) { - $input = $form_state->getValue(['static', 'selection']); - $entity_type = $form_state->getValue(['static', 'entity_type']); - $entity = $this->entityManager->getStorage($entity_type)->load($input); - $new_static = [ - 'machine_name' => $form_state->getValue(['static', 'machine_name']), - 'label' => $form_state->getValue(['static', 'label']), - 'type' => 'entity:' . $entity_type, - 'value' => $entity->uuid(), - ]; - - $static_context = (array) $this->entity->get('static_context'); - $static_context[] = $new_static; - $this->entity->set('static_context', $static_context); - $this->entity->save(); - } - } diff --git a/src/Form/StaticContextDeleteForm.php b/src/Form/StaticContextDeleteForm.php index 5440acc..425b699 100644 --- a/src/Form/StaticContextDeleteForm.php +++ b/src/Form/StaticContextDeleteForm.php @@ -24,7 +24,7 @@ class StaticContextDeleteForm extends ConfirmFormBase { protected $page; /** - * The static context settings array. + * The static context's machine name. * * @var array */ @@ -41,7 +41,7 @@ class StaticContextDeleteForm extends ConfirmFormBase { * {@inheritdoc} */ public function getQuestion() { - return $this->t('Are you sure you want to delete the static context %name?', ['%name' => $this->staticContext['label']]); + return $this->t('Are you sure you want to delete the static context %label?', ['%label' => $this->page->getStaticContext($this->staticContext)['label']]); } /** @@ -63,7 +63,7 @@ class StaticContextDeleteForm extends ConfirmFormBase { */ public function buildForm(array $form, FormStateInterface $form_state, PageInterface $page = NULL, $name = NULL) { $this->page = $page; - $this->staticContext = $page->getStaticContext($name); + $this->staticContext = $name; return parent::buildForm($form, $form_state); } @@ -71,9 +71,9 @@ class StaticContextDeleteForm extends ConfirmFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $this->page->removeStaticContext($this->staticContext['machine_name']); + drupal_set_message($this->t('The static context %label has been removed.', ['%label' => $this->page->getStaticContext($this->staticContext)['label']])); + $this->page->removeStaticContext($this->staticContext); $this->page->save(); - drupal_set_message($this->t('The static context %name has been removed.', ['%name' => $this->staticContext['label']])); $form_state->setRedirectUrl($this->getCancelUrl()); } diff --git a/src/Form/StaticContextEditForm.php b/src/Form/StaticContextEditForm.php index fbbfb0e..156932b 100644 --- a/src/Form/StaticContextEditForm.php +++ b/src/Form/StaticContextEditForm.php @@ -7,9 +7,8 @@ namespace Drupal\page_manager\Form; -use Drupal\Core\Condition\ConditionManager; use Drupal\Core\Form\FormStateInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\page_manager\PageInterface; /** * Provides a form for adding a new static context. @@ -40,27 +39,14 @@ class StaticContextEditForm extends StaticContextFormBase { /** * {@inheritdoc} */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $input = $form_state->getValue('selection'); - $entity_type = $form_state->getValue('entity_type'); - $entity = $this->getEntityFromSelection($entity_type, $input); - - $old_name = $this->staticContext['machine_name']; - - $this->staticContext = [ - 'machine_name' => $form_state->getValue('machine_name'), - 'label' => $form_state->getValue('label'), - 'type' => 'entity:' . $entity_type, - 'value' => $entity->uuid(), - ]; - - $this->page->updateStaticContext($old_name, $this->staticContext); - $this->page->save(); - - // Set the submission message. - drupal_set_message($this->submitMessageText()); - - $form_state->setRedirectUrl($this->page->urlInfo('edit-form')); + public function buildForm(array $form, FormStateInterface $form_state, PageInterface $page = NULL, $name = '') { + $form = parent::buildForm($form, $form_state, $page, $name); + // The machine name mustn't be changed after a static context was created. + $form['machine_name'] = array( + '#type' => 'value', + '#value' => $name, + ); + return $form; } } diff --git a/src/Form/StaticContextFormBase.php b/src/Form/StaticContextFormBase.php index b11c47f..b7b4cda 100644 --- a/src/Form/StaticContextFormBase.php +++ b/src/Form/StaticContextFormBase.php @@ -92,12 +92,12 @@ abstract class StaticContextFormBase extends FormBase { $form['machine_name'] = [ '#type' => 'machine_name', '#maxlength' => 64, - '#required' => FALSE, + '#required' => TRUE, '#machine_name' => [ 'exists' => [$this, 'contextExists'], 'source' => ['label'], ], - '#default_value' => $this->staticContext['machine_name'] ?: '', + '#default_value' => $name ? $name : '', ]; $form['entity_type'] = [ '#type' => 'select', @@ -114,8 +114,8 @@ abstract class StaticContextFormBase extends FormBase { ]; $entity = NULL; - if ($form_state->hasValue(['entity_type'])) { - $entity_type = $form_state->getValue(['entity_type']); + if ($form_state->hasValue('entity_type')) { + $entity_type = $form_state->getValue('entity_type'); if ($this->staticContext['value']) { $entity = $this->entityManager->loadEntityByUuid($entity_type, $this->staticContext['value']); } @@ -162,12 +162,11 @@ abstract class StaticContextFormBase extends FormBase { $entity = $this->getEntityFromSelection($entity_type, $selection); $this->staticContext = [ - 'machine_name' => $form_state->getValue('machine_name'), 'label' => $form_state->getValue('label'), 'type' => 'entity:' . $entity_type, 'value' => $entity->uuid(), ]; - $this->page->addStaticContext($this->staticContext); + $this->page->setStaticContext($form_state->getValue('machine_name'), $this->staticContext); $this->page->save(); // Set the submission message. diff --git a/src/PageInterface.php b/src/PageInterface.php index 7f09a09..e575999 100644 --- a/src/PageInterface.php +++ b/src/PageInterface.php @@ -104,17 +104,6 @@ interface PageInterface extends ConfigEntityInterface, EntityWithPluginCollectio public function getStaticContexts(); /** - * Adds a new static context to the page entity. - * - * @param array $configuration - * An array of configuration for the new static context. - * - * @return string - * The unique machine name of the static context. - */ - public function addStaticContext(array $configuration); - - /** * Retrieves a specific static context. * * @param string $name @@ -126,16 +115,16 @@ interface PageInterface extends ConfigEntityInterface, EntityWithPluginCollectio public function getStaticContext($name); /** - * Updates a specific static context. + * Adds/updates a given static context. * * @param string $name - * The static context unique name. + * The static context unique machine name. * @param array $configuration * A new array of configuration for the static context. * * @return $this */ - public function updateStaticContext($name, $configuration); + public function setStaticContext($name, $configuration); /** * Removes a specific static context. diff --git a/src/Tests/PageManagerAdminTest.php b/src/Tests/PageManagerAdminTest.php index 3d57cbe..e803d2a 100644 --- a/src/Tests/PageManagerAdminTest.php +++ b/src/Tests/PageManagerAdminTest.php @@ -59,7 +59,6 @@ class PageManagerAdminTest extends WebTestBase { $this->doTestAddBlockWithAjax(); $this->doTestEditBlock(); $this->doTestExistingPathWithoutParameters(); - $this->doTestStaticContext(); $this->doTestDeletePage(); } @@ -380,64 +379,6 @@ class PageManagerAdminTest extends WebTestBase { } /** - * Tests adding static context page. - */ - protected function doTestStaticContext() { - $this->drupalGet('admin/structure/page_manager/manage/foo'); - // Add new static context. - $this->clickLink(t('Add new static context')); - $edit = array( - 'machine_name' => 'test_context', - 'label' => 'Test context', - 'entity_type' => 'user', - 'selection' => 'administrator ' . '(' . $this->loggedInUser->id() . ')', - ); - $this->drupalPostForm(NULL, $edit, t('Add Static Context')); - $this->assertText(t('The Test context static context has been added.')); - - // Add a new display variant. - $this->clickLink(t('Add new display variant')); - $this->clickLink(t('Block page')); - $this->drupalPostForm(NULL, array('display_variant[label]' => 'Test display variant'), t('Add display variant')); - $this->assertText(t('he Test display variant display variant has been added.')); - - // Add new block with the static context. - $this->clickLink(t('Add new block')); - $this->clickLink(t('Entity view (User)')); - $this->drupalPostForm(NULL, array('region' => 'top', 'context_mapping[entity]' => 'test_context'), t('Add block')); - $this->drupalPostForm(NULL, NULL, t('Update display variant')); - $this->assertText(t('The Test display variant display variant has been updated.')); - - // Check the context in page. Viewing a user does not actually display - // anything we can use to verify it is the correct user. We test the block - // and the context's appearance. - $this->drupalGet('admin/foo'); - $this->assertText(t('Entity view (User)')); - $this->assertText(t('Member for')); - - // Edit the static context. - $this->drupalGet('admin/structure/page_manager/manage/foo'); - $this->clickLink(t('Edit')); - $edit = array( - 'machine_name' => 'test_context_edited', - 'label' => 'Test context edited', - 'entity_type' => 'user', - 'selection' => 'administrator ' . '(' . $this->loggedInUser->id() . ')', - ); - $this->drupalPostForm(NULL, $edit, t('Update Static Context')); - $this->assertText(t('The Test context edited static context has been updated.')); - $this->assertText(t('Test context edited')); - $this->assertText(t('test_context_edited')); - - // Remove the static context. - $this->clickLink(t('Delete')); - $this->drupalPostForm(NULL, NULL, t('Delete')); - $this->assertText(t('The static context Test context edited has been removed.')); - $this->drupalGet('admin/structure/page_manager/manage/foo'); - $this->assertNoText(t('Test context edited')); - } - - /** * Tests deleting a page. */ protected function doTestDeletePage() { diff --git a/src/Tests/StaticContextTest.php b/src/Tests/StaticContextTest.php index efe2620..d342655 100644 --- a/src/Tests/StaticContextTest.php +++ b/src/Tests/StaticContextTest.php @@ -117,6 +117,60 @@ class StaticContextTest extends WebTestBase { $this->assertText($node->get('body')->getValue()[0]['value']); $this->assertText($node2->label()); $this->assertText($node2->get('body')->getValue()[0]['value']); + + + + + // Change the second static context to the first node. + $this->drupalGet('admin/structure/page_manager/manage/' . $edit_page['id']); + $this->clickLink(t('Edit'), 1); + $edit = array( + 'label' => 'Static Node 2 edited', + 'entity_type' => 'node', + 'selection' => $node->getTitle(), + ); + $this->drupalPostForm(NULL, $edit, t('Update Static Context')); + $this->assertText('The ' . $edit['label'] . ' static context has been updated.'); + + // Open the page and verify that the node from the static context is there. + $this->drupalGet($edit_page['path']); + $this->assertText($node->label()); + $this->assertText($node->get('body')->getValue()[0]['value']); + // Also make sure the second node is NOT there. + $this->assertNoText($node2->label()); + $this->assertNoText($node2->get('body')->getValue()[0]['value']); + + // Change the first static context to the second node. + $this->drupalGet('admin/structure/page_manager/manage/' . $edit_page['id']); + $this->clickLink(t('Edit')); + $edit = array( + 'label' => 'Static Node edited', + 'entity_type' => 'node', + 'selection' => $node2->getTitle(), + ); + $this->drupalPostForm(NULL, $edit, t('Update Static Context')); + $this->assertText('The ' . $edit['label'] . ' static context has been updated.'); + + // Edit the page variant and remove one static context view block. + $this->clickLink(t('Edit'), 2); + // Remove the second static context view block from the display variant. + $this->clickLink(t('Delete'), 1); + $this->drupalPostForm(NULL, NULL, t('Delete')); + + // Make sure only the second static context's node is rendered on the page. + $this->drupalGet($edit_page['path']); + $this->assertNoText($node->label()); + $this->assertNoText($node->get('body')->getValue()[0]['value']); + $this->assertText($node2->label()); + $this->assertText($node2->get('body')->getValue()[0]['value']); + + // Delete a static context and verify that it was deleted. + $this->drupalGet('admin/structure/page_manager/manage/' . $edit_page['id']); + $this->clickLink(t('Delete')); + $this->drupalPostForm(NULL, NULL, t('Delete')); + $this->assertText('The static context ' . $edit['label'] . ' has been removed.'); + $this->drupalGet('admin/structure/page_manager/manage/' . $edit_page['id']); + $this->assertNoText($edit['label']); } }