FormatterPluginManager.php 4.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\field\Plugin\Type\Formatter\FormatterPluginManager..
 */

namespace Drupal\field\Plugin\Type\Formatter;

use Drupal\Component\Plugin\PluginManagerBase;
11
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
12 13
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
14
use Drupal\Core\Plugin\Discovery\AlterDecorator;
15 16
use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator;
use Drupal\field\FieldInstance;
17 18 19 20 21 22 23 24 25 26

/**
 * Plugin type manager for field formatters.
 */
class FormatterPluginManager extends PluginManagerBase {

  /**
   * Overrides Drupal\Component\Plugin\PluginManagerBase:$defaults.
   */
  protected $defaults = array(
27
    'field_types' => array(),
28 29 30 31 32
    'settings' => array(),
  );

  /**
   * Constructs a FormatterPluginManager object.
33 34 35
   *
   * @param array $namespaces
   *   An array of paths keyed by it's corresponding namespaces.
36
   */
37 38
  public function __construct($namespaces) {
    $this->discovery = new AnnotatedClassDiscovery('field', 'formatter', $namespaces);
39
    $this->discovery = new FormatterLegacyDiscoveryDecorator($this->discovery);
40
    $this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
41 42
    $this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info');
    $this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
43

44
    $this->factory = new FormatterFactory($this->discovery);
45 46 47 48
  }

  /**
   * Overrides PluginManagerBase::getInstance().
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   *
   * @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.
71 72
   */
  public function getInstance(array $options) {
73
    $configuration = $options['configuration'];
74
    $instance = $options['instance'];
75
    $field = field_info_field($instance['field_name']);
76

77 78 79 80 81 82 83
    // Fill in default configuration if needed.
    if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
      $configuration = $this->prepareConfiguration($field['type'], $configuration);
    }

    $plugin_id = $configuration['type'];

84 85 86
    // 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.
87
    $definition = $this->getDefinition($configuration['type']);
88 89 90
    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']);
91
      $plugin_id = $field_type_definition['default_formatter'];
92 93
    }

94
    $configuration += array(
95 96 97
      'instance' => $instance,
      'view_mode' => $options['view_mode'],
    );
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    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;
127 128 129
  }

}