Commit 4e979263 authored by catch's avatar catch

Issue #2026769 by fgm: Fixed Simplify DerivativeInterface implementations by...

Issue #2026769 by fgm: Fixed Simplify DerivativeInterface implementations by refactoring duplicated code to a base class.
parent fc38bee0
<?php
/**
* @file
* Contains Drupal\Component\Plugin\Derivative\DerivativeBase.
*/
namespace Drupal\Component\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
/**
* Default version of getDerivativeDefinition() common to most concrete
* implementations of DerivativeInterface.
*
* See the Aggregator and Mock block derivers for different implementations.
*/
abstract class DerivativeBase implements DerivativeInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = array();
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
return $this->derivatives;
}
}
......@@ -7,37 +7,14 @@
namespace Drupal\custom_block\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
/**
* Retrieves block plugin definitions for all custom blocks.
*/
class CustomBlock implements DerivativeInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*
* Retrieves a specific custom block definition from storage.
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
class CustomBlock extends DerivativeBase {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*
* Retrieves custom block definitions from storage.
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$custom_blocks = entity_load_multiple('custom_block');
......@@ -45,6 +22,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$this->derivatives[$custom_block->uuid->value] = $base_plugin_definition;
$this->derivatives[$custom_block->uuid->value]['admin_label'] = $custom_block->info->value;
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -6,7 +6,7 @@
namespace Drupal\block\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
/**
* Provides block plugin UI plugin definitions for all themes.
......@@ -15,37 +15,15 @@
*
* @see \Drupal\block\Plugin\system\plugin_ui\BlockPluginUI
*/
class BlockPluginUI implements DerivativeInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*
* @todo Add documentation to this method.
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
class BlockPluginUI extends DerivativeBase {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
// Provide a derivative of the plugin UI for each theme.
foreach (list_themes() as $key => $theme) {
$this->derivatives[$key] = $base_plugin_definition;
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -7,33 +7,14 @@
namespace Drupal\entity_reference\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
/**
* Base class for selection plugins provided by Entity Reference.
*/
class SelectionBase implements DerivativeInterface {
/**
* Holds the list of plugin derivatives.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
class SelectionBase extends DerivativeBase {
/**
* Implements DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$supported_entities = array(
......@@ -49,6 +30,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$this->derivatives[$entity_type]['label'] = t('@entity_type selection', array('@entity_type' => $info['label']));
}
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -7,33 +7,14 @@
namespace Drupal\language\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
/**
* Provides language switcher block plugin definitions for all languages.
*/
class LanguageBlock implements DerivativeInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
class LanguageBlock extends DerivativeBase {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
include_once DRUPAL_ROOT . '/core/includes/language.inc';
......@@ -43,7 +24,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$this->derivatives[$type]['admin_label'] = t('Language switcher (!type)', array('!type' => $info[$type]['name']));
$this->derivatives[$type]['cache'] = DRUPAL_NO_CACHE;
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -8,27 +8,19 @@
namespace Drupal\layout\Plugin\Derivative;
use DirectoryIterator;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
use Drupal\Core\Config\FileStorage;
/**
* Layout plugin derivative definition.
*
* Derivatives are an associative array keyed by 'provider__layoutname' where
* provider is the module or theme name and layoutname is the .yml filename,
* such as 'bartik__page' or 'layout__one-col'. The values of the array are
* associative arrays themselves with metadata about the layout such as
* 'template', 'css', 'admin css' and so on.
*/
class Layout implements DerivativeInterface {
/**
* List of derivatives.
*
* Associative array keyed by 'provider__layoutname' where provider is the
* module or theme name and layoutname is the .yml filename, such as
* 'bartik__page' or 'layout__one-col'. The values of the array are
* associative arrays themselves with metadata about the layout such as
* 'template', 'css', 'admin css' and so on.
*
* @var array
*/
protected $derivatives = array();
class Layout extends DerivativeBase {
/**
* Layout derivative type.
*
......@@ -41,18 +33,7 @@ class Layout implements DerivativeInterface {
protected $type = 'static';
/**
* Implements DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
/**
* Implements DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$available_layout_providers = array();
......@@ -85,7 +66,7 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$this->iterateDirectories($dir, $provider);
}
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
/**
......
......@@ -15,9 +15,8 @@
* @see \Drupal\menu\Plugin\block\block\MenuBlock
*/
class MenuBlock extends SystemMenuBlock {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
// Provide block plugin definitions for all user-defined (custom) menus.
......
......@@ -7,35 +7,16 @@
namespace Drupal\system\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
/**
* Provides block plugin definitions for system menus.
*
* @see \Drupal\system\Plugin\block\block\SystemMenuBlock
*/
class SystemMenuBlock implements DerivativeInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
class SystemMenuBlock extends DerivativeBase {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
// Provide a block plugin definition for each system menu.
......@@ -51,7 +32,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
$this->derivatives[$menu_key]['admin_label'] = !empty($entity) ? $entity->label() : $name;
$this->derivatives[$menu_key]['cache'] = DRUPAL_NO_CACHE;
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -7,33 +7,17 @@
namespace Drupal\views\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
use Drupal\views\Views;
/**
* A derivative class which provides automatic wizards for all base tables.
*
* The derivatives store all base table plugin information.
*/
class DefaultWizardDeriver implements DerivativeInterface {
class DefaultWizardDeriver extends DerivativeBase {
/**
* Stores all base table plugin information.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$views_data = Views::viewsData();
......@@ -50,8 +34,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
);
}
}
return $this->derivatives;
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment