class FormatterPluginManager

Plugin type manager for field formatters.

Hierarchy

Expanded class hierarchy of FormatterPluginManager

2 files declare their use of FormatterPluginManager
DisplayOverview.php in drupal/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php
Contains \Drupal\field_ui\DisplayOverview.
Field.php in drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php
Definition of Drupal\field\Plugin\views\field\Field.
1 string reference to 'FormatterPluginManager'
field.services.yml in drupal/core/modules/field/field.services.yml
drupal/core/modules/field/field.services.yml
1 service uses FormatterPluginManager

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php, line 20
Definition of Drupal\field\Plugin\Type\Formatter\FormatterPluginManager..

Namespace

Drupal\field\Plugin\Type\Formatter
View source
class FormatterPluginManager extends PluginManagerBase {

  /**
   * An array of formatter options for each field type.
   *
   * @var array
   */
  protected $formatterOptions;

  /**
   * Constructs a FormatterPluginManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations,
   */
  public function __construct(\Traversable $namespaces) {
    $annotation_namespaces = array(
      'Drupal\\field\\Annotation' => $namespaces['Drupal\\field'],
    );
    $this->discovery = new AnnotatedClassDiscovery('field/formatter', $namespaces, $annotation_namespaces, 'Drupal\\field\\Annotation\\FieldFormatter');
    $this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info');
    $this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
  }

  /**
   * {@inheritdoc}
   */
  public function createInstance($plugin_id, array $configuration) {
    $plugin_definition = $this->discovery
      ->getDefinition($plugin_id);
    $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
    return new $plugin_class($plugin_id, $plugin_definition, $configuration['instance'], $configuration['settings'], $configuration['label'], $configuration['view_mode']);
  }

  /**
   * Overrides PluginManagerBase::getInstance().
   *
   * @param array $options
   *   An array with the following key/value pairs:
   *   - instance: (FieldInstance) The field instance.
   *   - view_mode: (string) The view mode.
   *   - prepare: (bool, optional) Whether default values should get merged in
   *     the 'configuration' array. Defaults to TRUE.
   *   - configuration: (array) the configuration for the formatter. The
   *     following key value pairs are allowed, and are all optional if
   *     'prepare' is TRUE:
   *     - label: (string) Position of the label. The default 'field' theme
   *       implementation supports the values 'inline', 'above' and 'hidden'.
   *       Defaults to 'above'.
   *     - type: (string) The formatter to use. Defaults to the
   *       'default_formatter' for the field type, specified in
   *       hook_field_info(). The default formatter will also be used if the
   *       requested formatter is not available.
   *     - settings: (array) Settings specific to the formatter. Each setting
   *       defaults to the default value specified in the formatter definition.
   *
   * @return \Drupal\field\Plugin\Type\Formatter\FormatterInterface
   *   A formatter object.
   */
  public function getInstance(array $options) {
    $configuration = $options['configuration'];
    $instance = $options['instance'];
    $field = field_info_field($instance['field_name']);

    // Fill in default configuration if needed.
    if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
      $configuration = $this
        ->prepareConfiguration($field['type'], $configuration);
    }
    $plugin_id = $configuration['type'];

    // Switch back to default formatter if either:
    // - $type_info doesn't exist (the widget type is unknown),
    // - the field type is not allowed for the widget.
    $definition = $this
      ->getDefinition($configuration['type']);
    if (!isset($definition['class']) || !in_array($field['type'], $definition['field_types'])) {

      // Grab the default widget for the field type.
      $field_type_definition = field_info_field_types($field['type']);
      $plugin_id = $field_type_definition['default_formatter'];
    }
    $configuration += array(
      'instance' => $instance,
      'view_mode' => $options['view_mode'],
    );
    return $this
      ->createInstance($plugin_id, $configuration);
  }

  /**
   * Merges default values for formatter configuration.
   *
   * @param string $field_type
   *   The field type.
   * @param array $properties
   *   An array of formatter configuration.
   *
   * @return array
   *   The display properties with defaults added.
   */
  public function prepareConfiguration($field_type, array $configuration) {

    // Fill in defaults for missing properties.
    $configuration += array(
      'label' => 'above',
      'settings' => array(),
    );

    // If no formatter is specified, use the default formatter.
    if (!isset($configuration['type'])) {
      $field_type = field_info_field_types($field_type);
      $configuration['type'] = $field_type['default_formatter'];
    }

    // Fill in default settings values for the formatter.
    $configuration['settings'] += field_info_formatter_settings($configuration['type']);
    return $configuration;
  }

  /**
   * Returns an array of formatter options for a field type.
   *
   * @param string|null $field_type
   *   (optional) The name of a field type, or NULL to retrieve all formatters.
   *
   * @return array
   *   If no field type is provided, returns a nested array of all formatters,
   *   keyed by field type.
   */
  public function getOptions($field_type = NULL) {
    if (!isset($this->formatterOptions)) {
      $field_types = field_info_field_types();
      $options = array();
      foreach ($this
        ->getDefinitions() as $name => $formatter) {
        foreach ($formatter['field_types'] as $formatter_field_type) {

          // Check that the field type exists.
          if (isset($field_types[$formatter_field_type])) {
            $options[$formatter_field_type][$name] = $formatter['label'];
          }
        }
      }
      $this->formatterOptions = $options;
    }
    if ($field_type) {
      return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : array();
    }
    return $this->formatterOptions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterPluginManager::$formatterOptions protected property An array of formatter options for each field type.
FormatterPluginManager::createInstance public function Returns a preconfigured instance of a plugin. Overrides PluginManagerBase::createInstance
FormatterPluginManager::getInstance public function Overrides PluginManagerBase::getInstance(). Overrides PluginManagerBase::getInstance
FormatterPluginManager::getOptions public function Returns an array of formatter options for a field type.
FormatterPluginManager::prepareConfiguration public function Merges default values for formatter configuration.
FormatterPluginManager::__construct public function Constructs a FormatterPluginManager object.
PluginManagerBase::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 3
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions
PluginManagerBase::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
PluginManagerBase::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryInterface::getDefinitions
PluginManagerBase::processDefinition public function Performs extra processing on plugin definitions. 2