diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php index 85f615c..31e355c 100644 --- a/core/modules/block/lib/Drupal/block/BlockBase.php +++ b/core/modules/block/lib/Drupal/block/BlockBase.php @@ -126,7 +126,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( @@ -293,6 +293,46 @@ public function getCacheTags() { /** * {@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; + } + } + + /** + * {@inheritdoc} + */ public function getCacheBin() { return 'render'; } diff --git a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php index 4f6bdc2..bcc058f 100644 --- a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php +++ b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php @@ -124,7 +124,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 9e3b1b5..ca72621 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\Plugin\DataType\StringItem; /** * Provides a Block view builder. @@ -65,7 +66,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la // @todo Remove after fixing http://drupal.org/node/1989568. '#block' => $entity, ); - $build[$entity_id]['#configuration']['label'] = check_plain($configuration['label']); + $build[$entity_id]['#configuration']['label'] = String::checkPlain($plugin->getTitle()); // Set cache tags; these always need to be set, whether the block is // cacheable or not, so that the page cache is correctly informed. 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 2648919..677f051 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -34,6 +34,9 @@ public function build() { $output['#title'] = Xss::filterAdmin($this->view->getTitle()); } + // 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); @@ -113,4 +116,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; + } + }