FieldTypePluginManager.php 3.35 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\FieldTypePluginManager.
6 7
 */

8
namespace Drupal\Core\Field;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager;

/**
 * Plugin manager for 'field type' plugins.
 */
class FieldTypePluginManager extends DefaultPluginManager {

  /**
   * {@inheritdoc}
   */
  protected $defaults = array(
    'settings' => array(),
    'instance_settings' => array(),
  );

  /**
   * Constructs the FieldTypePluginManager 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\Language\LanguageManager $language_manager
   *   The language manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface
   *   The module handler.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
42
    parent::__construct('Plugin/Field/FieldType', $namespaces, 'Drupal\Core\Field\Annotation\FieldType');
43
    $this->alterInfo($module_handler, 'field_info');
44
    $this->setCacheBackend($cache_backend, $language_manager, 'field_types_plugins');
45 46 47 48 49 50

    // @todo Remove once all core field types have been converted (see
    // http://drupal.org/node/2014671).
    $this->discovery = new LegacyFieldTypeDiscoveryDecorator($this->discovery, $module_handler);
  }

51 52 53 54 55 56 57
  /**
   * {@inheritdoc}
   */
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);
    if (!isset($definition['list_class'])) {
      if ($definition['configurable']) {
58
        $definition['list_class'] = '\Drupal\Core\Field\ConfigFieldItemList';
59 60
      }
      else {
61
        $definition['list_class'] = '\Drupal\Core\Field\FieldItemList';
62 63 64 65
      }
    }
  }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  /**
   * Returns the default field-level settings for a field type.
   *
   * @param string $type
   *   A field type name.
   *
   * @return array
   *   The type's default settings, as provided by the plugin
   *   definition, or an empty array if type or settings are undefined.
   */
  public function getDefaultSettings($type) {
    $info = $this->getDefinition($type);
    return isset($info['settings']) ? $info['settings'] : array();
  }

  /**
   * Returns the default instance-level settings for a field type.
   *
   * @param string $type
   *   A field type name.
   *
   * @return array
   *   The instance's default settings, as provided by the plugin
   *   definition, or an empty array if type or settings are undefined.
   */
  public function getDefaultInstanceSettings($type) {
    $info = $this->getDefinition($type);
    return isset($info['instance_settings']) ? $info['instance_settings'] : array();
  }

96 97 98 99 100 101 102 103 104 105 106 107 108
  /**
   * Gets the definition of all field types that are configurable.
   *
   * @return array
   *   An array of field type definitions.
   */
  public function getConfigurableDefinitions() {
    $definitions = $this->getDefinitions();
    return array_filter($definitions, function ($definition) {
      return $definition['configurable'];
    });
  }

109
}