Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\Annotation\Block.
*/
namespace Drupal\Core\Block\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Block annotation object.
*
* @ingroup block_api
*
* @Annotation
*/
class Block extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The administrative label of the block.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $admin_label = '';
/**
* The category in the admin UI where the block will be listed.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $category = '';
/**
* Class used to retrieve derivative definitions of the block.
*
* @var string
*/
public $derivative = '';
}

View file

@ -0,0 +1,323 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\BlockBase.
*/
namespace Drupal\Core\Block;
use Drupal\block\BlockInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Transliteration\TransliterationInterface;
/**
* 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.
*
* @ingroup block_api
*/
abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface {
/**
* The transliteration service.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* {@inheritdoc}
*/
public function label() {
if (!empty($this->configuration['label'])) {
return $this->configuration['label'];
}
$definition = $this->getPluginDefinition();
// Cast the admin label to a string since it is an object.
// @see \Drupal\Core\StringTranslation\TranslationWrapper
return (string) $definition['admin_label'];
}
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep(
$this->baseConfigurationDefaults(),
$this->defaultConfiguration(),
$configuration
);
}
/**
* Returns generic default configuration for block plugins.
*
* @return array
* An associative array with the default configuration.
*/
protected function baseConfigurationDefaults() {
return array(
'id' => $this->getPluginId(),
'label' => '',
'provider' => $this->pluginDefinition['provider'],
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'cache' => array(
// Blocks are cacheable by default.
'max_age' => Cache::PERMANENT,
),
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array();
}
/**
* {@inheritdoc}
*/
public function setConfigurationValue($key, $value) {
$this->configuration[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = $this->blockAccess($account);
return $return_as_object ? $access : $access->isAllowed();
}
/**
* Indicates whether the block should be shown.
*
* Blocks with specific access checking should override this method rather
* than access(), in order to avoid repeating the handling of the
* $return_as_object argument.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user session for which to check access.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*
* @see self::access()
*/
protected function blockAccess(AccountInterface $account) {
// By default, the block is visible.
return AccessResult::allowed();
}
/**
* {@inheritdoc}
*
* Creates a generic configuration form for all block types. Individual
* block plugins can add elements to this form by overriding
* BlockBase::blockForm(). Most block plugins should not override this
* method unless they need to alter the generic form elements.
*
* @see \Drupal\Core\Block\BlockBase::blockForm()
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$definition = $this->getPluginDefinition();
$form['provider'] = array(
'#type' => 'value',
'#value' => $definition['provider'],
);
$form['admin_label'] = array(
'#type' => 'item',
'#title' => $this->t('Block description'),
'#markup' => SafeMarkup::checkPlain($definition['admin_label']),
);
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->label(),
'#required' => TRUE,
);
$form['label_display'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Display title'),
'#default_value' => ($this->configuration['label_display'] === BlockInterface::BLOCK_LABEL_VISIBLE),
'#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE,
);
// Identical options to the ones for page caching.
// @see \Drupal\system\Form\PerformanceForm::buildForm()
$period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400);
$period = array_map(array(\Drupal::service('date.formatter'), 'formatInterval'), array_combine($period, $period));
$period[0] = '<' . $this->t('no caching') . '>';
$period[\Drupal\Core\Cache\Cache::PERMANENT] = $this->t('Forever');
$form['cache'] = array(
'#type' => 'details',
'#title' => $this->t('Cache settings'),
);
$form['cache']['max_age'] = array(
'#type' => 'select',
'#title' => $this->t('Maximum age'),
'#description' => $this->t('The maximum time this block may be cached.'),
'#default_value' => $this->configuration['cache']['max_age'],
'#options' => $period,
);
// Add plugin-specific settings for this block type.
$form += $this->blockForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
return array();
}
/**
* {@inheritdoc}
*
* Most block plugins should not override this method. To add validation
* for a specific block type, override BlockBase::blockValidate().
*
* @see \Drupal\Core\Block\BlockBase::blockValidate()
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// Remove the admin_label form item element value so it will not persist.
$form_state->unsetValue('admin_label');
$this->blockValidate($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function blockValidate($form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*
* Most block plugins should not override this method. To add submission
* handling for a specific block type, override BlockBase::blockSubmit().
*
* @see \Drupal\Core\Block\BlockBase::blockSubmit()
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Process the block's submission handling if no errors occurred only.
if (!$form_state->getErrors()) {
$this->configuration['label'] = $form_state->getValue('label');
$this->configuration['label_display'] = $form_state->getValue('label_display');
$this->configuration['provider'] = $form_state->getValue('provider');
$this->configuration['cache'] = $form_state->getValue('cache');
$this->blockSubmit($form, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function getMachineNameSuggestion() {
$definition = $this->getPluginDefinition();
$admin_label = $definition['admin_label'];
// @todo This is basically the same as what is done in
// \Drupal\system\MachineNameController::transliterate(), so it might make
// sense to provide a common service for the two.
$transliterated = $this->transliteration()->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
$replace_pattern = '[^a-z0-9_.]+';
$transliterated = Unicode::strtolower($transliterated);
if (isset($replace_pattern)) {
$transliterated = preg_replace('@' . $replace_pattern . '@', '', $transliterated);
}
return $transliterated;
}
/**
* Wraps the transliteration service.
*
* @return \Drupal\Component\Transliteration\TransliterationInterface
*/
protected function transliteration() {
if (!$this->transliteration) {
$this->transliteration = \Drupal::transliteration();
}
return $this->transliteration;
}
/**
* Sets the transliteration service.
*
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* The transliteration service.
*/
public function setTransliteration(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return (int)$this->configuration['cache']['max_age'];
}
}

View file

@ -0,0 +1,86 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\BlockManager.
*/
namespace Drupal\Core\Block;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Manages discovery and instantiation of block plugins.
*
* @todo Add documentation to this class.
*
* @see \Drupal\Core\Block\BlockPluginInterface
*/
class BlockManager extends DefaultPluginManager implements BlockManagerInterface, FallbackPluginManagerInterface {
use CategorizingPluginManagerTrait {
getSortedDefinitions as traitGetSortedDefinitions;
getGroupedDefinitions as traitGetGroupedDefinitions;
}
use ContextAwarePluginManagerTrait;
/**
* Constructs a new \Drupal\Core\Block\BlockManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Block', $namespaces, $module_handler, 'Drupal\Core\Block\BlockPluginInterface', 'Drupal\Core\Block\Annotation\Block');
$this->alterInfo('block');
$this->setCacheBackend($cache_backend, 'block_plugins');
}
/**
* {@inheritdoc}
*/
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
$this->processDefinitionCategory($definition);
}
/**
* {@inheritdoc}
*/
public function getSortedDefinitions(array $definitions = NULL) {
// Sort the plugins first by category, then by label.
$definitions = $this->traitGetSortedDefinitions($definitions, 'admin_label');
// Do not display the 'broken' plugin in the UI.
unset($definitions['broken']);
return $definitions;
}
/**
* {@inheritdoc}
*/
public function getGroupedDefinitions(array $definitions = NULL) {
$definitions = $this->traitGetGroupedDefinitions($definitions, 'admin_label');
// Do not display the 'broken' plugin in the UI.
unset($definitions[$this->t('Block')]['broken']);
return $definitions;
}
/**
* {@inheritdoc}
*/
public function getFallbackPluginId($plugin_id, array $configuration = array()) {
return 'broken';
}
}

View file

@ -0,0 +1,18 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\BlockManagerInterface.
*/
namespace Drupal\Core\Block;
use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface;
/**
* Provides an interface for the discovery and instantiation of block plugins.
*/
interface BlockManagerInterface extends ContextAwarePluginManagerInterface, CategorizingPluginManagerInterface {
}

View file

@ -0,0 +1,151 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\BlockPluginInterface.
*/
namespace Drupal\Core\Block;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;
/**
* 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
* brief references to the important components that are not coupled to the
* interface.
*
* @ingroup block_api
*/
interface BlockPluginInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, CacheableDependencyInterface, DerivativeInspectionInterface {
/**
* Returns the user-facing block label.
*
* @todo Provide other specific label-related methods in
* https://www.drupal.org/node/2025649.
*
* @return string
* The block label.
*/
public function label();
/**
* Indicates whether the block should be shown.
*
* This method allows base implementations to add general access restrictions
* that should apply to all extending block plugins.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user session for which to check access.
* @param bool $return_as_object
* (optional) Defaults to FALSE.
*
* @return bool|\Drupal\Core\Access\AccessResultInterface
* The access result. Returns a boolean if $return_as_object is FALSE (this
* is the default) and otherwise an AccessResultInterface object.
* When a boolean is returned, the result of AccessInterface::isAllowed() is
* returned, i.e. TRUE means access is explicitly allowed, FALSE means
* access is either explicitly forbidden or "no opinion".
*
* @see \Drupal\block\BlockAccessControlHandler
*/
public function access(AccountInterface $account, $return_as_object = FALSE);
/**
* Builds and returns the renderable array for this block plugin.
*
* @return array
* A renderable array representing the content of the block.
*
* @see \Drupal\block\BlockViewBuilder
*/
public function build();
/**
* Sets a particular value in the block settings.
*
* @param string $key
* The key of PluginBase::$configuration to set.
* @param mixed $value
* The value to set for the provided key.
*
* @todo This doesn't belong here. Move this into a new base class in
* https://www.drupal.org/node/1764380.
* @todo This does not set a value in \Drupal::config(), so the name is confusing.
*
* @see \Drupal\Component\Plugin\PluginBase::$configuration
*/
public function setConfigurationValue($key, $value);
/**
* Returns the configuration form elements specific to this block plugin.
*
* Blocks that need to add form elements to the normal block configuration
* form should implement this method.
*
* @param array $form
* The form definition array for the block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array $form
* The renderable form array representing the entire configuration form.
*/
public function blockForm($form, FormStateInterface $form_state);
/**
* Adds block type-specific validation for the block form.
*
* Note that this method takes the form structure and form state for the full
* block configuration form as arguments, not just the elements defined in
* BlockPluginInterface::blockForm().
*
* @param array $form
* The form definition array for the full block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\Core\Block\BlockPluginInterface::blockForm()
* @see \Drupal\Core\Block\BlockPluginInterface::blockSubmit()
*/
public function blockValidate($form, FormStateInterface $form_state);
/**
* Adds block type-specific submission handling for the block form.
*
* Note that this method takes the form structure and form state for the full
* block configuration form as arguments, not just the elements defined in
* BlockPluginInterface::blockForm().
*
* @param array $form
* The form definition array for the full block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\Core\Block\BlockPluginInterface::blockForm()
* @see \Drupal\Core\Block\BlockPluginInterface::blockValidate()
*/
public function blockSubmit($form, FormStateInterface $form_state);
/**
* Suggests a machine name to identify an instance of this block.
*
* The block plugin need not verify that the machine name is at all unique. It
* is only responsible for providing a baseline suggestion; calling code is
* responsible for ensuring whatever uniqueness is required for the use case.
*
* @return string
* The suggested machine name.
*/
public function getMachineNameSuggestion();
}

View file

@ -0,0 +1,27 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\MainContentBlockPluginInterface.
*/
namespace Drupal\Core\Block;
/**
* The interface for "main page content" blocks.
*
* A main page content block represents the content returns by the controller.
*
* @ingroup block_api
*/
interface MainContentBlockPluginInterface extends BlockPluginInterface {
/**
* Sets the main content render array.
*
* @param array $main_content
* The render array representing the main content.
*/
public function setMainContent(array $main_content);
}

View file

@ -0,0 +1,20 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\MessagesBlockPluginInterface.
*/
namespace Drupal\Core\Block;
/**
* The interface for "messages" (#type => status_messages) blocks.
*
* @see drupal_set_message()
* @see drupal_get_message()
* @see \Drupal\Core\Render\Element\StatusMessages
* @see \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
*
* @ingroup block_api
*/
interface MessagesBlockPluginInterface extends BlockPluginInterface { }

View file

@ -0,0 +1,52 @@
<?php
/**
* @file
* Contains \Drupal\Core\Block\Plugin\Block\Broken.
*/
namespace Drupal\Core\Block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a fallback plugin for missing block plugins.
*
* @Block(
* id = "broken",
* admin_label = @Translation("Broken/Missing"),
* category = @Translation("Block"),
* )
*/
class Broken extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return $this->brokenMessage();
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
return $this->brokenMessage();
}
/**
* Generate message with debugging information as to why the block is broken.
*
* @return array
* Render array containing debug information.
*/
protected function brokenMessage() {
$build['message'] = [
'#markup' => $this->t('This block is broken or missing. You may be missing content or you might need to enable the original module.')
];
return $build;
}
}