diff --git a/src/Annotation/PanelsStyle.php b/src/Annotation/PanelsStyle.php index dce598f..84b9e19 100644 --- a/src/Annotation/PanelsStyle.php +++ b/src/Annotation/PanelsStyle.php @@ -27,4 +27,18 @@ class PanelsStyle extends Plugin { */ public $description = ''; + /** + * Whether or not this style plugin can be used to render blocks. + * + * @var string + */ + public $block = TRUE; + + /** + * Whether or not this style plugin can be used to render regions. + * + * @var string + */ + public $region = TRUE; + } diff --git a/src/Form/PanelsBlockConfigureFormBase.php b/src/Form/PanelsBlockConfigureFormBase.php index ac5b575..c5a2e4e 100644 --- a/src/Form/PanelsBlockConfigureFormBase.php +++ b/src/Form/PanelsBlockConfigureFormBase.php @@ -146,10 +146,7 @@ abstract class PanelsBlockConfigureFormBase extends FormBase { '#required' => TRUE, ]; - $plugin_options = []; - foreach (\Drupal::service('plugin.manager.panels.style')->getDefinitions() as $definition) { - $plugin_options[$definition['id']] = $definition['title']; - } + $plugin_options = \Drupal::service('plugin.manager.panels.style')->getPluginOptions('block'); $form['style'] = [ '#type' => 'select', diff --git a/src/Form/PanelsRegionEditFormBase.php b/src/Form/PanelsRegionEditFormBase.php index 4cea941..399d102 100644 --- a/src/Form/PanelsRegionEditFormBase.php +++ b/src/Form/PanelsRegionEditFormBase.php @@ -122,10 +122,7 @@ abstract class PanelsRegionEditFormBase extends FormBase { $form['#tree'] = TRUE; - $plugin_options = []; - foreach (\Drupal::service('plugin.manager.panels.style')->getDefinitions() as $definition) { - $plugin_options[$definition['id']] = $definition['title']; - } + $plugin_options = \Drupal::service('plugin.manager.panels.style')->getPluginOptions('region'); $form['style'] = [ '#type' => 'select', diff --git a/src/Plugin/PanelsStyle/PanelsStyleBase.php b/src/Plugin/PanelsStyle/PanelsStyleBase.php index dd2d655..7edcd04 100644 --- a/src/Plugin/PanelsStyle/PanelsStyleBase.php +++ b/src/Plugin/PanelsStyle/PanelsStyleBase.php @@ -18,7 +18,7 @@ use Drupal\panels\Plugin\PanelsStyle\PanelsStyleInterface; /** * Base class for all style plugins. */ -class PanelsStyleBase extends PluginBase implements PanelsStyleInterface { +abstract class PanelsStyleBase extends PluginBase implements PanelsStyleInterface { /** * The style configuration. diff --git a/src/Plugin/PanelsStyle/PanelsStyleInterface.php b/src/Plugin/PanelsStyle/PanelsStyleInterface.php index 82ae83d..f086fd8 100644 --- a/src/Plugin/PanelsStyle/PanelsStyleInterface.php +++ b/src/Plugin/PanelsStyle/PanelsStyleInterface.php @@ -4,13 +4,14 @@ namespace Drupal\panels\Plugin\PanelsStyle; use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Block\BlockPluginInterface; use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant; /** * Provides an interface for PanelsStyle plugins. */ -interface PanelsStyleInterface extends ConfigurablePluginInterface, PluginInspectionInterface { +interface PanelsStyleInterface extends ConfigurablePluginInterface, PluginInspectionInterface, PluginFormInterface { /** * Renders a region. diff --git a/src/Plugin/PanelsStyle/PanelsStyleList.php b/src/Plugin/PanelsStyle/PanelsStyleList.php index 5b251ea..eacde33 100644 --- a/src/Plugin/PanelsStyle/PanelsStyleList.php +++ b/src/Plugin/PanelsStyle/PanelsStyleList.php @@ -3,13 +3,15 @@ namespace Drupal\panels\Plugin\PanelsStyle; use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant; +use Drupal\Core\Form\FormStateInterface; /** * Defines the list panels style plugin. * * @PanelsStyle( * id = "panels_list", - * title = @Translation("List") + * title = @Translation("List"), + * block = FALSE * ) */ class PanelsStyleList extends PanelsStyleBase { @@ -17,14 +19,46 @@ class PanelsStyleList extends PanelsStyleBase { /** * {@inheritdoc} */ + public function defaultConfiguration() { + return [ + 'type' => 'ul', + ]; + } + + /** + * {@inheritdoc} + */ public function buildRegion(PanelsDisplayVariant $display, array $build, $region, array $blocks) { + + $config = $this->getConfiguration(); + $build = [ '#theme' => 'item_list', - '#list_type' => 'ul', + '#list_type' => $config['type'], '#items' => $build, ]; return parent::buildRegion($display, $build, $region, $blocks); } + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + + $config = $this->getConfiguration(); + + $form['type'] = [ + '#type' => 'select', + '#title' => t('Type'), + '#options' => [ + 'ul' => 'Unordered', + 'ol' => 'Ordered', + ], + '#default_value' => $config['type'], + ]; + + return $form; + } + } diff --git a/src/Plugin/PanelsStyle/PanelsStylePluginManager.php b/src/Plugin/PanelsStyle/PanelsStylePluginManager.php index b1a01f4..4497828 100644 --- a/src/Plugin/PanelsStyle/PanelsStylePluginManager.php +++ b/src/Plugin/PanelsStyle/PanelsStylePluginManager.php @@ -31,4 +31,17 @@ class PanelsStylePluginManager extends DefaultPluginManager implements PanelsSty $this->alterInfo('panels_style_plugin'); } + /** + * {inheritdoc} + */ + public function getPluginOptions($type = null) { + $options = []; + foreach ($this->getDefinitions() as $definition) { + if (!$type || !empty($definition[$type])) { + $options[$definition['id']] = $definition['title']; + } + } + return $options; + } + } diff --git a/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php b/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php index 79b63f4..a767892 100644 --- a/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php +++ b/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php @@ -9,4 +9,15 @@ use Drupal\Component\Plugin\PluginManagerInterface; */ interface PanelsStylePluginManagerInterface extends PluginManagerInterface { + /** + * Get the style plugin options. + * + * @param string $type + * The type of the panels component being styled. Options are 'block' or + * 'region'. All style plugins will be returned if not provided. + * @return array + * An array of definition titles, keyed by + */ + public function getPluginOptions($type = null); + } diff --git a/tests/src/Unit/StandardDisplayBuilderTest.php b/tests/src/Unit/StandardDisplayBuilderTest.php index 6c02518..18aa618 100644 --- a/tests/src/Unit/StandardDisplayBuilderTest.php +++ b/tests/src/Unit/StandardDisplayBuilderTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Utility\Token; +use Drupal\Core\Layout\LayoutDefinition; use Drupal\Core\Layout\LayoutDefault; use Drupal\panels\Plugin\DisplayBuilder\DisplayBuilderManagerInterface; use Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder; @@ -138,8 +139,17 @@ class StandardDisplayBuilderTest extends UnitTestCase { $this->conditionManager = $this->prophesize(ConditionManager::class); $this->builderManager = $this->prophesize(DisplayBuilderManagerInterface::class); + $this->layoutDefinition = $this->getMockBuilder(LayoutDefinition::class) + ->setConstructorArgs([[]]) + ->setMethods([ + 'getRegionNames', + 'getLibrary', + 'getThemeHook', + ]) + ->getMock(); + $this->layout = $this->getMockBuilder(LayoutDefault::class) - ->setConstructorArgs([[], 'layout_plugin_default', []]) + ->setConstructorArgs([[], 'layout_plugin_default', $this->layoutDefinition]) ->setMethods(['getRegionDefinitions']) ->getMock(); @@ -220,6 +230,10 @@ class StandardDisplayBuilderTest extends UnitTestCase { 'footer' => [], ]); + $this->layoutDefinition->method('getRegionNames')->willReturn(array_keys($regions)); + $this->layoutDefinition->method('getLibrary')->willReturn(''); + $this->layoutDefinition->method('getThemeHook')->willReturn('container'); + $this->variant->method('getBlockStyle')->willReturn($this->style); $this->variant->method('getRegionAssignments')->willReturn($regions); $this->variant->method('getRegionNames')->willReturn([