diff --git a/src/Plugin/PanelsStyle/PanelsStyleBase.php b/src/Plugin/PanelsStyle/PanelsStyleBase.php index ec3d1d7..9c04f9e 100644 --- a/src/Plugin/PanelsStyle/PanelsStyleBase.php +++ b/src/Plugin/PanelsStyle/PanelsStyleBase.php @@ -12,18 +12,30 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Render\Element; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant; use Drupal\panels\Plugin\PanelsStyle\PanelsStyleInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for all style plugins. */ -abstract class PanelsStyleBase extends PluginBase implements PanelsStyleInterface { +abstract class PanelsStyleBase extends PluginBase implements PanelsStyleInterface, ContainerFactoryPluginInterface { use StringTranslationTrait; + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * The style configuration. * @@ -34,9 +46,22 @@ abstract class PanelsStyleBase extends PluginBase implements PanelsStyleInterfac /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->setConfiguration($configuration); + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('module_handler') + ); } /** @@ -55,18 +80,47 @@ abstract class PanelsStyleBase extends PluginBase implements PanelsStyleInterfac */ public function buildBlock(PanelsDisplayVariant $display, BlockPluginInterface $block) { $build = [ - '#theme' => 'block', - '#attributes' => ['class' => ['container']], - '#weight' => $display->getWeight(), - '#configuration' => $block->getConfiguration(), - '#plugin_id' => $block->getPluginId(), - '#base_plugin_id' => $block->getBaseId(), - '#derivative_plugin_id' => $block->getDerivativeId(), - ]; + '#theme' => 'block', + '#attributes' => ['class' => ['container']], + '#contextual_links' => [], + '#weight' => $display->getWeight(), + '#configuration' => $block->getConfiguration(), + '#plugin_id' => $block->getPluginId(), + '#base_plugin_id' => $block->getBaseId(), + '#derivative_plugin_id' => $block->getDerivativeId(), + ]; if (!empty($build['#configuration']['label'])) { $build['#configuration']['label'] = SafeMarkup::checkPlain($build['#configuration']['label']); } - $build['content'] = $block->build(); + + // Build the block and bubble its attributes up if possible. This + // allows modules like Quickedit to function. + // See \Drupal\block\BlockViewBuilder::preRender() for reference. + $content = $block->build(); + $this->moduleHandler->alter(['block_build', 'block_build_' . $block->getBaseId()], $content, $block); + if ($content !== NULL && !Element::isEmpty($content)) { + foreach (['#attributes', '#contextual_links'] as $property) { + if (isset($content[$property])) { + $build[$property] += $content[$property]; + unset($content[$property]); + } + } + } + + // If the block is empty, instead of trying to render the block + // correctly return just #cache, so that the render system knows the + // reasons (cache contexts & tags) why this block is empty. + if (Element::isEmpty($content)) { + $build = []; + $cacheable_metadata = CacheableMetadata::createFromObject($build); + $cacheable_metadata->applyTo($build); + if (isset($content['#cache'])) { + $build['#cache'] += $content['#cache']; + } + } + + $build['content'] = $content; + $this->moduleHandler->alter(['block_view', 'block_view_' . $block->getBaseId()], $build, $block); return $build; }