diff --git a/config/install/panels.settings.yml b/config/install/panels.settings.yml new file mode 100644 index 0000000..3d572ec --- /dev/null +++ b/config/install/panels.settings.yml @@ -0,0 +1,2 @@ +default_style_region: panels_default +default_style_block: panels_default diff --git a/config/schema/panels.schema.yml b/config/schema/panels.schema.yml index 8adb932..56be3b9 100644 --- a/config/schema/panels.schema.yml +++ b/config/schema/panels.schema.yml @@ -1,3 +1,39 @@ +panels.settings: + type: config_object + mapping: + default_style_region: + type: string + label: Default Region Style + default_style_block: + type: string + label: Default Block Style + +panels_style: + type: mapping + mapping: + plugin: + type: string + label: Plugin ID + configuration: + type: panels_style.plugin.[%parent.plugin] + +panels_style.plugin.panels_list: + type: mapping + mapping: + type: + type: string + label: Type + +panels_style.plugin.panels_default: + type: mapping + +panels.block_plugin.*: + type: ctools.block_plugin.[id] + mapping: + style: + type: panels_style + label: Style + display_variant.plugin.panels_variant: type: ctools.block_display_variant label: 'Panels display variant' @@ -20,4 +56,15 @@ display_variant.plugin.panels_variant: storage_id: type: string label: Storage Id - + blocks: + sequence: + - type: panels.block_plugin.[id] + regions: + type: sequence + label: Regions + sequence: + type: mapping + mapping: + style: + type: panels_style + label: Style diff --git a/panels.install b/panels.install index 6a67704..1f51302 100644 --- a/panels.install +++ b/panels.install @@ -1,5 +1,7 @@ uninstall(['layout_plugin'], FALSE); \Drupal::service('module_installer')->install(['layout_discovery'], FALSE); } -} \ No newline at end of file +} + +/** + * Set default values for panels styles + */ +function panels_update_8402() { + $config_path = file_get_contents(drupal_get_path('module', 'panels') . '/config/install/panels.settings.yml'); + $data = Yaml::parse($config_path); + \Drupal::configFactory()->getEditable('panels.settings')->setData($data)->save(TRUE); +} 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..0e758d7 100644 --- a/src/Form/PanelsBlockConfigureFormBase.php +++ b/src/Form/PanelsBlockConfigureFormBase.php @@ -146,15 +146,12 @@ 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', '#title' => $this->t('Style Plugin'), - '#description' => $this->t('Please select the style plugin for this region.'), + '#description' => $this->t('Please select the style plugin for this block.'), '#options' => $plugin_options, '#default_value' => $this->stylePlugin->getPluginId(), '#ajax' => array( 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/DisplayVariant/PanelsDisplayVariant.php b/src/Plugin/DisplayVariant/PanelsDisplayVariant.php index b0a90c7..0c67a02 100644 --- a/src/Plugin/DisplayVariant/PanelsDisplayVariant.php +++ b/src/Plugin/DisplayVariant/PanelsDisplayVariant.php @@ -7,6 +7,7 @@ use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Block\BlockManager; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Condition\ConditionManager; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Layout\LayoutInterface; @@ -81,6 +82,13 @@ class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardIn protected $styleManager; /** + * The configuration manager. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface; + */ + protected $configFactory; + + /** * Constructs a new PanelsDisplayVariant. * * @param array $configuration @@ -108,11 +116,12 @@ class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardIn * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager * The layout plugin manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, BlockManager $block_manager, ConditionManager $condition_manager, ModuleHandlerInterface $module_handler, DisplayBuilderManagerInterface $builder_manager, LayoutPluginManagerInterface $layout_manager, PanelsStylePluginManagerInterface $style_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, ConfigFactoryInterface $config_factory, BlockManager $block_manager, ConditionManager $condition_manager, ModuleHandlerInterface $module_handler, DisplayBuilderManagerInterface $builder_manager, LayoutPluginManagerInterface $layout_manager, PanelsStylePluginManagerInterface $style_manager) { $this->moduleHandler = $module_handler; $this->builderManager = $builder_manager; $this->layoutManager = $layout_manager; $this->styleManager = $style_manager; + $this->configFactory = $config_factory; parent::__construct($configuration, $plugin_id, $plugin_definition, $context_handler, $account, $uuid_generator, $token, $block_manager, $condition_manager); } @@ -129,6 +138,7 @@ class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardIn $container->get('current_user'), $container->get('uuid'), $container->get('token'), + $container->get('config.factory'), $container->get('plugin.manager.block'), $container->get('plugin.manager.condition'), $container->get('module_handler'), @@ -321,8 +331,9 @@ class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardIn */ public function getBlockStyle(BlockPluginInterface $block, $style_id = '') { $block_config = $block->getConfiguration(); + $default = $this->configFactory->get('panels.settings')->get('default_style_block'); - $plugin_id = !empty($block_config['style']['plugin']) ? $block_config['style']['plugin'] : 'panels_default'; + $plugin_id = !empty($block_config['style']['plugin']) ? $block_config['style']['plugin'] : $default; $plugin_configuration = !empty($block_config['style']['configuration']) ? $block_config['style']['configuration'] : []; if (!empty($style_id) && $style_id !== $plugin_id) { @@ -384,8 +395,9 @@ class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardIn */ public function getRegionStyle($region, $style_id = '') { $region_config = !empty($this->configuration['regions'][$region]) ? $this->configuration['regions'][$region] : []; + $default = $this->configFactory->get('panels.settings')->get('default_style_region'); - $plugin_id = !empty($region_config['style']['plugin']) ? $region_config['style']['plugin'] : 'panels_default'; + $plugin_id = !empty($region_config['style']['plugin']) ? $region_config['style']['plugin'] : $default; $plugin_configuration = !empty($region_config['style']['configuration']) ? $region_config['style']['configuration'] : []; if (!empty($style_id) && $style_id !== $plugin_id) { 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/PanelsStyleBroken.php b/src/Plugin/PanelsStyle/PanelsStyleBroken.php new file mode 100644 index 0000000..5f312af --- /dev/null +++ b/src/Plugin/PanelsStyle/PanelsStyleBroken.php @@ -0,0 +1,52 @@ +alert('THe panels style plugin named "@style" is missing for region "@region"', [ + '@region' => $region, + '@style' => $display->getConfiguration()['regions'][$region]['style']['plugin'], + ]); + return parent::buildRegion($display, $build, $region, $blocks); + } + + /** + * {@inheritdoc} + */ + public function buildBlock(PanelsDisplayVariant $display, BlockPluginInterface $block) { + \Drupal::logger('panels')->alert('The panels style plugin named "@style" is missing for block "@block"', [ + '@block' => $block->label(), + '@style' => $block->getConfiguration()['style']['plugin'], + ]); + return parent::buildBlock($display, $block); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['broken_message'] = [ + '#markup' => t('The panels style plugin is missing for this block or region.'), + ]; + return $form; + } + +} 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..e7ad7a1 100644 --- a/src/Plugin/PanelsStyle/PanelsStylePluginManager.php +++ b/src/Plugin/PanelsStyle/PanelsStylePluginManager.php @@ -31,4 +31,36 @@ 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; + } + + /** + * {@inheritdoc} + */ + public function getDefinitions() { + $definitions = parent::getDefinitions(); + // Don't return the fallback plugin definition + if (isset($definitions['panels_broken'])) { + unset($definitions['panels_broken']); + } + return $definitions; + } + + /** + * {inheritdoc} + */ + public function getFallbackPluginId($plugin_id, array $configuration = []) { + return 'panels_broken'; + } + } diff --git a/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php b/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php index 79b63f4..38c156e 100644 --- a/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php +++ b/src/Plugin/PanelsStyle/PanelsStylePluginManagerInterface.php @@ -3,10 +3,22 @@ namespace Drupal\panels\Plugin\PanelsStyle; use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Component\Plugin\FallbackPluginManagerInterface; /** * Provides an interface for the discovery and instantiation of layout plugins. */ -interface PanelsStylePluginManagerInterface extends PluginManagerInterface { +interface PanelsStylePluginManagerInterface extends PluginManagerInterface, FallbackPluginManagerInterface { + + /** + * 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/PanelsDisplayVariantTest.php b/tests/src/Unit/PanelsDisplayVariantTest.php index f0fd6f1..24125db 100644 --- a/tests/src/Unit/PanelsDisplayVariantTest.php +++ b/tests/src/Unit/PanelsDisplayVariantTest.php @@ -14,6 +14,7 @@ use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Utility\Token; +use Drupal\Core\Config\ConfigFactory; use Drupal\panels\Plugin\DisplayBuilder\DisplayBuilderManagerInterface; use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant; Use Drupal\panels\Plugin\PanelsStyle\PanelsStyleInterface; @@ -87,6 +88,12 @@ class PanelsDisplayVariantTest extends UnitTestCase { $this->contextHandler = $this->prophesize(ContextHandlerInterface::class); $this->uuidGenerator = $this->prophesize(UuidInterface::class); $this->token = $this->prophesize(Token::class); + $this->configFactory = $this->getConfigFactoryStub([ + 'panels.settings' => [ + 'default_style_block' => 'panels_default', + 'default_style_region' => 'panels_default', + ], + ]); $this->blockManager = $this->prophesize(BlockManager::class); $this->conditionManager = $this->prophesize(ConditionManager::class); $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class); @@ -104,7 +111,7 @@ class PanelsDisplayVariantTest extends UnitTestCase { ->createInstance(Argument::type('string'), Argument::type('array')) ->willReturn($this->style->reveal()); - $this->variant = new PanelsDisplayVariant([], '', [], $this->contextHandler->reveal(), $this->account->reveal(), $this->uuidGenerator->reveal(), $this->token->reveal(), $this->blockManager->reveal(), $this->conditionManager->reveal(), $this->moduleHandler->reveal(), $this->builderManager->reveal(), $this->layoutManager->reveal(), $this->styleManager->reveal()); + $this->variant = new PanelsDisplayVariant([], '', [], $this->contextHandler->reveal(), $this->account->reveal(), $this->uuidGenerator->reveal(), $this->token->reveal(), $this->configFactory, $this->blockManager->reveal(), $this->conditionManager->reveal(), $this->moduleHandler->reveal(), $this->builderManager->reveal(), $this->layoutManager->reveal(), $this->styleManager->reveal()); } /** diff --git a/tests/src/Unit/StandardDisplayBuilderTest.php b/tests/src/Unit/StandardDisplayBuilderTest.php index 6c02518..b953ce8 100644 --- a/tests/src/Unit/StandardDisplayBuilderTest.php +++ b/tests/src/Unit/StandardDisplayBuilderTest.php @@ -10,6 +10,8 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Utility\Token; +use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\Layout\LayoutDefinition; use Drupal\Core\Layout\LayoutDefault; use Drupal\panels\Plugin\DisplayBuilder\DisplayBuilderManagerInterface; use Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder; @@ -134,12 +136,27 @@ class StandardDisplayBuilderTest extends UnitTestCase { $this->uuidGenerator = $this->prophesize(UuidInterface::class); $this->token = $this->prophesize(Token::class); + $this->configFactory = $this->getConfigFactoryStub([ + 'panels.settings' => [ + 'default_style_block' => 'panels_default', + 'default_style_region' => 'panels_default', + ], + ]); $this->blockManager = $this->prophesize(BlockManager::class); $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(); @@ -164,6 +181,7 @@ class StandardDisplayBuilderTest extends UnitTestCase { $this->account->reveal(), $this->uuidGenerator->reveal(), $this->token->reveal(), + $this->configFactory, $this->blockManager->reveal(), $this->conditionManager->reveal(), $this->moduleHandler->reveal(), @@ -220,6 +238,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([