Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Created June 21, 2013 02:39
Show Gist options
  • Select an option

  • Save sdboyer/5828466 to your computer and use it in GitHub Desktop.

Select an option

Save sdboyer/5828466 to your computer and use it in GitHub Desktop.
additional title methods
<?php
/**
* @file
* Contains \Drupal\block\BlockBase.
*/
namespace Drupal\block;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Asset\AssetBag;
use Drupal\block\Plugin\Core\Entity\Block;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
/**
* Defines a base block implementation that most blocks plugins will extend.
*
* This abstract class provides the generic block configuration form, default
* block settings, and handling for general user-defined block visibility
* settings.
*/
abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
final public function getRawTitle() {
// Override titles should supersede native title logic.
if (isset($this->configuration['label'])) {
return $this->configuration['label'];
}
else {
return $this->doGetRawTitle();
}
}
/**
* Returns a title for the block based on some native block logic.
*
* This method is intended for overriding by subclasses in order to provide
* their own logic for generating the block title.
*
* @see BlockBase::getTitle()
*
* @return string
*/
protected function doGetRawTitle() {
// @todo make this abstract instead
return '';
}
/**
* {@inheritdoc}
*/
public function getTitle() {
$title = $this->getRawTitle();
if ($this->tokenizeTitle()) {
$tokenizer = \Drupal::service('token');
$title = $tokenizer->replace($title, $this->getContextValues());
}
return $title;
}
/**
* Indicates whether title processing should include tokenization.
*
* Child classes should override this method and have it return TRUE if
* title tokenization, utilizing injected context data, is desired.
*
* @return bool
*/
protected function tokenizeTitle() {
return FALSE;
}
}
<?php
/**
* @file
* Contains \Drupal\block\BlockPluginInterface.
*/
namespace Drupal\block;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
/**
* Defines the required interface for all block plugins.
*
* @todo Add detailed documentation here explaining the block system's
* architecture and the relationships between the various objects, including
* brif references to the important components that are not coupled to the
* interface.
*
* @see \Drupal\block\BlockBase
*/
interface BlockPluginInterface extends ContextAwarePluginInterface {
/**
* Gets a raw, unprocessed user-facing title for this block.
*
* The title returned from this function should be a raw string that,
* if appropriate for the block, has not been run through any type of
* tokenization or placeholder substitution.
*
* This string may contain user input, and should be filtered accordingly.
*
* @return string
*/
public function getRawTitle();
/**
* Returns the fully processed user-facing title for this block.
*
* This string may contain user input, and should be filtered accordingly.
*
* @return string
*/
public function getTitle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment