LegacyFieldTypeDiscoveryDecorator.php 2.33 KB
Newer Older
1
2
3
4
<?php

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

8
namespace Drupal\Core\Field;
9
10
11
12
13
14
15
16

use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

/**
 * Custom decorator to add legacy field types.
 *
 * Legacy field types are discovered through the old hook_field_info() hook,
17
 * and handled by the Drupal\field\Plugin\Field\FieldType\LegacyConfigFieldItem class.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * @todo Remove once all core field types have been converted (see
 * http://drupal.org/node/2014671).
 */
class LegacyFieldTypeDiscoveryDecorator implements DiscoveryInterface {

  /**
   * The decorated discovery object.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $decorated;

  /**
32
   * Creates a \Drupal\Core\Field\LegacyFieldTypeDiscoveryDecorator object.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
   *
   * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
   *   The parent object implementing DiscoveryInterface that is being
   *   decorated.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(DiscoveryInterface $decorated, ModuleHandlerInterface $module_handler) {
    $this->decorated = $decorated;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinition($plugin_id) {
    $definitions = $this->getDefinitions();
    return isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinitions() {
    $definitions = $this->decorated->getDefinitions();

59
    // We cannot use HookDiscovery, since it uses
60
    // \Drupal::moduleHandler()->getImplementations(), which
61
62
63
64
65
    // throws exceptions during upgrades.
    foreach (array_keys($this->moduleHandler->getModuleList()) as $module) {
      $function = $module . '_field_info';
      if (function_exists($function)) {
        foreach ($function() as $plugin_id => $definition) {
66
67
68
          $definition['id'] = $plugin_id;
          $definition['provider'] = $module;
          $definition['configurable'] = TRUE;
69
          $definition += array('list_class' => '\Drupal\Core\Field\Plugin\Field\FieldType\LegacyConfigFieldItemList');
70
71
72
73
74
75
76
77
78
          $definitions[$plugin_id] = $definition;
        }
      }
    }

    return $definitions;
  }

}