diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php index 48e469b..cce1ac7 100644 --- a/core/modules/block/lib/Drupal/block/BlockBase.php +++ b/core/modules/block/lib/Drupal/block/BlockBase.php @@ -99,7 +99,7 @@ public function buildConfigurationForm(array $form, array &$form_state) { '#type' => 'textfield', '#title' => $this->t('Title'), '#maxlength' => 255, - '#default_value' => !empty($this->configuration['label']) ? $this->configuration['label'] : $definition['admin_label'], + '#default_value' => !empty($this->configuration['label']) ? $this->configuration['label'] : $this->getDefaultTitle(), '#required' => TRUE, ); $form['label_display'] = array( @@ -185,4 +185,44 @@ public function getMachineNameSuggestion() { return $transliterated; } + /** + * {@inheritdoc} + */ + public function getDefaultTitle() { + // @todo this connects the wrong dots, but it works for a first-draft patch. + $definition = $this->getPluginDefinition(); + return $definition['admin_label']; + } + + /** + * {@inheritdoc} + */ + public function getRawTitle() { + $configuration = $this->getConfiguration(); + // Override titles should supersede native title logic. + return isset($configuration['label']) ? $configuration['label'] : $this->getDefaultTitle(); + } + + /** + * {@inheritdoc} + */ + public function getTitle() { + return $this->getRawTitle(); + // @todo add tokenization based on context as default approach + // once https://drupal.org/node/2024783 is in. + } + + /** + * {@inheritdoc} + */ + public function isTitleVisible() { + // Assume the title is visible if the setting does not exist. + if (!isset($this->configuration['label_display'])) { + return TRUE; + } + else { + return $this->configuration['label_display'] === BLOCK_LABEL_VISIBLE; + } + } + } diff --git a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php index 047efd9..d255015 100644 --- a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php +++ b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php @@ -123,7 +123,70 @@ public function blockSubmit($form, &$form_state); * * @return string * The suggested machine name. + */ + public function getMachineNameSuggestion(); + + /** + * Gets the default, unprocessed, frontend-facing title for this block. + * + * This title should be the block's suggestion of a sane title for frontend + * display to the end user. If this block's title changes based on injected + * contextual data, then this string should contain the relevant tokens. + * + * It must be possible to produce this title with neither configuration nor + * contextual data having been injected into the block. + * + * The block is responsible for filtering this string appropriately if it + * could contain user input. + * + * @return string + */ + public function getDefaultTitle(); + + /** + * Gets the unprocessed user-facing title for this block. + * + * This method should allow user configuration to modify or fully replace the + * suggested title returned from BlockPluginInterface::getDefaultTitle() + * based on user configuration. As with that default title, returning a string + * with raw tokens in it is appropriate. + * + * If the relevant configuration is not present, or if the Block chooses not + * to allow any user modification of the title output, this method should + * simply defer to BlockPluginInterface::getDefaultTitle(). + * + * This string may contain user input, and should be filtered accordingly. + * + * @return string + * + * @see \Drupal\Block\BlockPluginInterface::getDefaultTitle() + */ + public function getRawTitle(); + + /** + * Gets the fully-prepared user-facing title for this block. + * + * In the simple case, this method should take the output of + * BlockPluginInterface::getRawTitle(), apply any transformational logic to it + * that requires injected contextual data, and return the result. + * + * This method should not be called unless a block has had both configuration + * and all necessary contexts properly injected. + * + * This string may contain user input, and should be filtered accordingly. + * + * @return string + * + * @see \Drupal\Block\BlockPluginInterface::getDefaultTitle() + * @see \Drupal\Block\BlockPluginInterface::getRawTitle() + */ + public function getTitle(); + + /** + * Indicates whether the block title should be shown on the frontend. + * + * @return bool */ - public function getMachineNameSuggestion(); + public function isTitleVisible(); } diff --git a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php index bfc136b..977b4b8 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -9,7 +9,9 @@ use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityInterface; - +use Drupal\Core\Entity\Plugin\DataType\StringItem; +use Drupal\Component\Utility\String; + /** * Provides a Block view builder. */ @@ -47,7 +49,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la '#configuration' => $configuration, '#plugin_id' => $plugin_id, ); - $build[$entity_id]['#configuration']['label'] = check_plain($configuration['label']); + $build[$entity_id]['#configuration']['label'] = String::checkPlain($plugin->getTitle()); } else { $build[$entity_id] = array(); diff --git a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php index dac57a0..7dcf7ac 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -31,6 +31,7 @@ public function build() { $this->view->display_handler->preBlockBuild($this); if ($output = $this->view->executeDisplay($this->displayID)) { + // Set the label to the title configured in the view. if (empty($this->configuration['views_label'])) { $this->configuration['label'] = Xss::filterAdmin($this->view->getTitle()); @@ -39,6 +40,10 @@ public function build() { $this->configuration['label'] = $this->configuration['views_label']; } $this->configuration['label_display'] = TRUE; + + // Store the title as an object property for later retrieval via method. + $this->outputTitle = $this->configuration['label'];/ + // Before returning the block output, convert it to a renderable array // with contextual links. $this->addContextualLinks($output); @@ -99,4 +104,19 @@ public function getMachineNameSuggestion() { return 'views_block__' . $this->view->storage->id() . '_' . $this->view->current_display; } + /** + * {@inheritdoc} + */ + public function getTitle() { + // The only canonical way to build the title for a view is to ask the view + // for it directly, once the view has been executed. To avoid executing the + // view unnecessarily, we do it only in build(), then set a property for use + // here. So, if that property is not set, we must call build() to set it. + if (is_null($this->outputTitle)) { + $this->build(); + } + + return $this->outputTitle; + } + }