Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active December 15, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdboyer/5312082 to your computer and use it in GitHub Desktop.
Save sdboyer/5312082 to your computer and use it in GitHub Desktop.
still not quite done, but a better example
<?php
/**
* @file
* Contains \Drupal\block\Plugin\Core\Entity\Block.
*/
namespace Drupal\block\Plugin\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Config\Entity\EmbeddedPluginConfigInterface;
class Block extends ConfigEntityBase implements EmbeddedPluginConfigInterface {
// just demonstrating what it implements, so everything else is cut out
}
<?php
/**
* @file
* Contains \Drupal\block\BlockInterface.
*/
namespace Drupal\block;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\ConfigEntityAwarePluginInterface;
/**
* 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 BlockInterface extends ContextAwarePluginInterface, PluginInspectionInterface, ConfigEntityAwarePluginInterface {
// no need to see these here, just demonstrating what these implement
}
<?php
/**
* Contains \Drupal\block\Plugin\Type\BlockManager.
*/
namespace Drupal\block\Plugin\Type;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Plugin\Mapper\ConfigDrivenMapperInterface;
use Drupal\block\BlockInterface;
use Drupal\block\Plugin\Core\Entity\Block;
/**
* Manages discovery and instantiation of block plugins.
*
* @todo Add documentation to this class.
*
* @see \Drupal\block\BlockInterface
*/
class BlockManager extends PluginManagerBase implements ConfigDrivenMapperInterface {
protected $defaults = array(
// 'semantic_wrapper' => 'div', @todo uncomment this once semantic markup wrappers are implemented
'has_native_access_logic' => TRUE,
// Indicates whether or not a block can be safely deferred to a subrequest.
'subrequest_safe' => FALSE, // @todo it'd be great to default to TRUE here.
);
/**
* Constructs a new \Drupal\block\Plugin\Type\BlockManager object.
*
* @param array $namespaces
* An array of paths keyed by it's corresponding namespaces.
*/
public function __construct(array $namespaces) {
$this->discovery = new AnnotatedClassDiscovery('block', 'block', $namespaces);
$this->discovery = new DerivativeDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'block');
$this->discovery = new CacheDecorator($this->discovery, 'block_plugins:' . language(LANGUAGE_TYPE_INTERFACE)->langcode, 'cache_block', CacheBackendInterface::CACHE_PERMANENT, array('block'));
}
/**
* {@inheritdoc}
* // @todo implement an interface that gets rid of the configuration array entirely.
*
* @return \Drupal\block\BlockInterface
* A configured BlockInterface instance.
*/
public function createInstance($plugin_id, array $configuration = array(), Block $entity = NULL) {
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $this->discovery);
return new $plugin_class($configuration, $plugin_id, $this->discovery, $entity);
}
/**
* {@inheritdoc}
*
* @return \Drupal\block\BlockInterface
* A configured BlockInterface instance.
*/
public function getInstanceFromConfigId($config_id, $plugin_id = '') {
// Allow for different prefixes - e.g., custom vs. generic reusable blocks.
$entity = config_load_entity_by_name($config_id);
$plugin_id = $plugin_id ?: $entity->getPluginId();
return $this->createInstance($plugin_id, array(), $entity);
}
}
<?php
/**
* @file
* Contains Drupal\Core\Plugin\Mapper\ConfigDrivenMapperInterface.
*/
namespace Drupal\Core\Plugin\Mapper;
use Drupal\Component\Plugin\Exception\PluginException;
/**
* Defines an interface for retrieving plugins via a config id.
*
* For plugins that use the config system for their configuration CRUD, most
* calling code will have only a config id, not a plugin id. The plugin id is
* only stored in the configuration referenced by the config id.
*
* The most useful interface pattern for these cases is to simply allow calling
* code to load the plugin by providing the config id.
*/
interface ConfigDrivenMapperInterface {
/**
* Gets a configured plugin instance from a configuration id.
*
* The calling code may optionally also specify a plugin id
*
* @param string $config_id
* The id of the configuration to load.
*
* @param string $plugin_id
* Optional. If provided, this plugin id will be used instead of whatever is
* specified by the loaded configuration.
*
* @return object
*
* @throws \InvalidArgumentException
*/
public function getInstanceFromConfigId($config_id, $plugin_id = '');
}
<?php
/**
* @file
* Contains Drupal\Core\Plugin\ConfigEntityAwarePluginInterface.
*/
namespace Drupal\Core\Plugin;
use Drupal\Core\Config\Entity\EmbeddedPluginConfigInterface;
/**
* Interface for plugins that use ConfigEntity for their configuration storage.
*/
interface ConfigEntityAwarePluginInterface {
/**
* Sets the ConfigEntity for this plugin.
*
* @param EmbeddedPluginConfigInterface $config
* The ConfigEntity object.
*
* @return void
*/
public function setConfig(EmbeddedPluginConfigInterface $config);
/**
* Returns the ConfigEntity for this plugin.
*
* @return EmbeddedPluginConfigInterface
*/
public function getConfig();
/**
* Returns the string identifier of the instance id.
*
* This is typically, though not necessarily, the full name of the
* configuration object that backs the plugin's ConfigEntity.
*
* @return string
*/
public function getInstanceId();
}
<?php
/**
* @file
* Contains Drupal\Core\Config\Entity\EmbeddedPluginConfigInterface.
*/
namespace Drupal\Core\Config\Entity;
/**
* Interface for ConfigEntities that are used as embedded config by plugins.
*/
interface EmbeddedPluginConfigInterface extends ConfigEntityInterface {
/**
* Returns the id of the plugin in which this configuration is embedded.
*
* @return string
*/
public function getPluginId();
/**
* Sets the id of the plugin in which this configuration is embedded.
*
* @param string $plugin_id
* The id of the plugin.
*
* @return void
*/
public function setPluginId($plugin_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment