Commit 58ba3690 authored by tim.plunkett's avatar tim.plunkett Committed by EclipseGc

Issue #2646286 by tim.plunkett, EclipseGc: Remove selection logic/conditions...

Issue #2646286 by tim.plunkett, EclipseGc: Remove selection logic/conditions from the BlockDisplayVariant
parent a73040e1
......@@ -39,15 +39,6 @@ ctools.block_display_variant:
type: display_variant.plugin
label: 'Block display variant'
mapping:
selection_logic:
type: string
label: 'Selection logic'
selection_conditions:
type: sequence
label: 'Selection Conditions'
sequence:
- type: condition.plugin.[id]
label: 'Selection Condition'
blocks:
type: sequence
label: 'Blocks'
......
<?php
/**
* @file
* Contains \Drupal\ctools\Plugin\ConditionVariantInterface.
*/
namespace Drupal\ctools\Plugin;
use Drupal\Core\Display\VariantInterface;
/**
* Provides an interface for variant plugins that use condition plugins.
*/
interface ConditionVariantInterface extends VariantInterface {
/**
* Gets the values for all defined contexts.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of set contexts, keyed by context name.
*/
public function getContexts();
/**
* Returns the conditions used for determining if this variant is selected.
*
* @return \Drupal\Core\Condition\ConditionInterface[]|\Drupal\Core\Condition\ConditionPluginCollection
* An array of configured condition plugins.
*/
public function getSelectionConditions();
/**
* Removes a specific selection condition.
*
* @param string $condition_id
* The selection condition ID.
*
* @return $this
*/
public function removeSelectionCondition($condition_id);
/**
* Adds a new selection condition.
*
* @param array $configuration
* An array of configuration for the new selection condition.
*
* @return string
* The selection condition ID.
*/
public function addSelectionCondition(array $configuration);
/**
* Returns the logic used to compute selections, either 'and' or 'or'.
*
* @return string
* The string 'and', or the string 'or'.
*/
public function getSelectionLogic();
/**
* Returns the definition of the plugin implementation.
*
* @return array
* The plugin definition, as returned by the discovery object used by the
* plugin manager.
*/
public function getPluginDefinition();
/**
* Sets the context values for this display variant.
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* An array of contexts, keyed by context name.
*
* @return $this
*/
public function setContexts(array $contexts);
/**
* Retrieves a specific selection condition.
*
* @param string $condition_id
* The selection condition ID.
*
* @return \Drupal\Core\Condition\ConditionInterface
* The selection condition object.
*/
public function getSelectionCondition($condition_id);
}
<?php
/**
* @file
* Contains \Drupal\ctools\Plugin\ConditionVariantTrait.
*/
namespace Drupal\ctools\Plugin;
use Drupal\Core\Condition\ConditionAccessResolverTrait;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
/**
* Provides methods for \Drupal\ctools\Plugin\ConditionVariantInterface.
*/
trait ConditionVariantTrait {
use ConditionAccessResolverTrait;
/**
* The condition plugin manager.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $conditionManager;
/**
* The plugin collection that holds the selection condition plugins.
*
* @var \Drupal\Component\Plugin\LazyPluginCollection
*/
protected $selectionConditionCollection;
/**
* Gets the condition plugin manager.
*
* @return \Drupal\Core\Condition\ConditionManager
* The condition plugin manager.
*/
protected function getConditionManager() {
if (!$this->conditionManager) {
$this->conditionManager = \Drupal::service('plugin.manager.condition');
}
return $this->conditionManager;
}
/**
* @see \Drupal\ctools\Plugin\ConditionVariantInterface::getSelectionConditions()
*/
public function getSelectionConditions() {
if (!$this->selectionConditionCollection) {
$this->selectionConditionCollection = new ConditionPluginCollection($this->getConditionManager(), $this->getSelectionConfiguration());
}
return $this->selectionConditionCollection;
}
/**
* @see \Drupal\ctools\Plugin\ConditionVariantInterface::addSelectionCondition()
*/
public function addSelectionCondition(array $configuration) {
$configuration['uuid'] = $this->uuidGenerator()->generate();
$this->getSelectionConditions()->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
/**
* @see \Drupal\ctools\Plugin\ConditionVariantInterface::getSelectionCondition()
*/
public function getSelectionCondition($condition_id) {
return $this->getSelectionConditions()->get($condition_id);
}
/**
* @see \Drupal\ctools\Plugin\ConditionVariantInterface::removeSelectionCondition()
*/
public function removeSelectionCondition($condition_id) {
$this->getSelectionConditions()->removeInstanceId($condition_id);
return $this;
}
/**
* Determines if the selection conditions will pass given a set of contexts.
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* An array of set contexts, keyed by context name.
*
* @return bool
* TRUE if access is granted, FALSE otherwise.
*/
protected function determineSelectionAccess(array $contexts) {
$conditions = $this->getSelectionConditions();
foreach ($conditions as $condition) {
if ($condition instanceof ContextAwarePluginInterface) {
$this->contextHandler()->applyContextMapping($condition, $contexts);
}
}
return $this->resolveConditions($conditions, $this->getSelectionLogic());
}
/**
* @see \Drupal\ctools\Plugin\ConditionVariantInterface::getSelectionLogic()
*/
abstract public function getSelectionLogic();
/**
* Returns the configuration for stored selection conditions.
*
* @return array
* An array of condition configuration, keyed by the unique condition ID.
*/
abstract protected function getSelectionConfiguration();
/**
* Returns the UUID generator.
*
* @return \Drupal\Component\Uuid\UuidInterface
*/
abstract protected function uuidGenerator();
/**
* Returns the context handler.
*
* @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
*/
abstract protected function contextHandler();
}
......@@ -10,7 +10,6 @@ namespace Drupal\ctools\Plugin\DisplayVariant;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Block\BlockManager;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Display\VariantBase;
use Drupal\Core\Display\ContextAwareVariantInterface;
......@@ -22,18 +21,15 @@ use Drupal\Core\Utility\Token;
use Drupal\ctools\Form\AjaxFormTrait;
use Drupal\ctools\Plugin\BlockVariantInterface;
use Drupal\ctools\Plugin\BlockVariantTrait;
use Drupal\ctools\Plugin\ConditionVariantInterface;
use Drupal\ctools\Plugin\ConditionVariantTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a base class for a display variant that simply contains blocks.
*/
abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVariantInterface, ConditionVariantInterface, ContainerFactoryPluginInterface, BlockVariantInterface, RefinableCacheableDependencyInterface {
abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVariantInterface, ContainerFactoryPluginInterface, BlockVariantInterface, RefinableCacheableDependencyInterface {
use AjaxFormTrait;
use BlockVariantTrait;
use ConditionVariantTrait;
/**
* The context handler.
......@@ -124,22 +120,12 @@ abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVa
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account = NULL) {
// Delegate to the conditions.
return $this->determineSelectionAccess($this->getContexts());
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'blocks' => [],
'selection_conditions' => [],
'selection_logic' => 'and',
'blocks' => []
];
}
......@@ -150,9 +136,6 @@ abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVa
foreach ($this->getBlockCollection() as $instance) {
$this->calculatePluginDependencies($instance);
}
foreach ($this->getSelectionConditions() as $instance) {
$this->calculatePluginDependencies($instance);
}
return $this->dependencies;
}
......@@ -161,7 +144,6 @@ abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVa
*/
public function getConfiguration() {
return [
'selection_conditions' => $this->getSelectionConditions()->getConfiguration(),
'blocks' => $this->getBlockCollection()->getConfiguration(),
] + parent::getConfiguration();
}
......@@ -171,18 +153,10 @@ abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVa
*/
public function setConfiguration(array $configuration) {
parent::setConfiguration($configuration);
$this->getSelectionConditions()->setConfiguration($this->configuration['selection_conditions']);
$this->getBlockCollection()->setConfiguration($this->configuration['blocks']);
return $this;
}
/**
* {@inheritdoc}
*/
public function getSelectionLogic() {
return $this->configuration['selection_logic'];
}
/**
* Gets the contexts.
*
......@@ -213,13 +187,6 @@ abstract class BlockDisplayVariant extends VariantBase implements ContextAwareVa
return $this->contextHandler;
}
/**
* {@inheritdoc}
*/
protected function getSelectionConfiguration() {
return $this->configuration['selection_conditions'];
}
/**
* {@inheritdoc}
*/
......
......@@ -26,31 +26,6 @@ use Drupal\Tests\UnitTestCase;
*/
class BlockDisplayVariantTest extends UnitTestCase {
/**
* Tests the access() method.
*
* @covers ::access
*/
public function testAccess() {
$display_variant = $this->getMockBuilder(TestBlockDisplayVariant::class)
->disableOriginalConstructor()
->setMethods(['determineSelectionAccess'])
->getMock();
$display_variant->expects($this->once())
->method('determineSelectionAccess')
->willReturn(FALSE);
$this->assertSame(FALSE, $display_variant->access());
$display_variant = $this->getMockBuilder(TestBlockDisplayVariant::class)
->disableOriginalConstructor()
->setMethods(['determineSelectionAccess'])
->getMock();
$display_variant->expects($this->once())
->method('determineSelectionAccess')
->willReturn(TRUE);
$this->assertSame(TRUE, $display_variant->access());
}
/**
* Tests the submitConfigurationForm() method.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment