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

/**
 * @file
 * Contains \Drupal\edit\EditorSelector.
 */

namespace Drupal\edit;

use Drupal\Component\Plugin\PluginManagerInterface;
11
use Drupal\Component\Utility\NestedArray;
12
use Drupal\field\Plugin\Core\Entity\FieldInstance;
13
14

/**
15
 * Selects an in-place editor (an Editor plugin) for a field.
16
17
18
19
 */
class EditorSelector implements EditorSelectorInterface {

  /**
20
   * The manager for editor (Create.js PropertyEditor widget) plugins.
21
22
23
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
24
  protected $editorManager;
25
26

  /**
27
   * A list of alternative editor plugin IDs, keyed by editor plugin ID.
28
   *
29
   * @var array
30
   */
31
  protected $alternatives;
32
33
34
35

  /**
   * Constructs a new EditorSelector.
   *
36
37
   * @param \Drupal\Component\Plugin\PluginManagerInterface
   *   The manager for Create.js PropertyEditor widget plugins.
38
   */
39
40
  public function __construct(PluginManagerInterface $editor_manager) {
    $this->editorManager = $editor_manager;
41
42
43
  }

  /**
44
   * {@inheritdoc}
45
46
   */
  public function getEditor($formatter_type, FieldInstance $instance, array $items) {
47
48
49
50
51
52
53
54
55
56
57
58
59
    // Build a static cache of the editors that have registered themselves as
    // alternatives to a certain editor.
    if (!isset($this->alternatives)) {
      $editors = $this->editorManager->getDefinitions();
      foreach ($editors as $alternative_editor_id => $editor) {
        if (isset($editor['alternativeTo'])) {
          foreach ($editor['alternativeTo'] as $original_editor_id) {
            $this->alternatives[$original_editor_id][] = $alternative_editor_id;
          }
        }
      }
    }

60
61
62
63
64
65
    // Check if the formatter defines an appropriate in-place editor. For
    // example, text formatters displaying untrimmed text can choose to use the
    // 'direct' editor. If the formatter doesn't specify, fall back to the
    // 'form' editor, since that can work for any field. Formatter definitions
    // can use 'disabled' to explicitly opt out of in-place editing.
    $formatter_info = field_info_formatter_types($formatter_type);
66
67
    $editor_id = isset($formatter_info['edit']['editor']) ? $formatter_info['edit']['editor'] : 'form';
    if ($editor_id === 'disabled') {
68
69
      return;
    }
70
71
72
    elseif ($editor_id === 'form') {
      return 'form';
    }
73

74
75
76
77
78
79
80
81
82
83
84
    // No early return, so create a list of all choices.
    $editor_choices = array($editor_id);
    if (isset($this->alternatives[$editor_id])) {
      $editor_choices = array_merge($editor_choices, $this->alternatives[$editor_id]);
    }

    // Make a choice.
    foreach ($editor_choices as $editor_id) {
      $editor = $this->editorManager->createInstance($editor_id);
      if ($editor->isCompatible($instance, $items)) {
        return $editor_id;
85
86
87
      }
    }

88
89
    // We still don't have a choice, so fall back to the default 'form' editor.
    return 'form';
90
91
92
  }

  /**
93
   * {@inheritdoc}
94
   */
95
  public function getEditorAttachments(array $editor_ids) {
96
    $attachments = array();
97
    $editor_ids = array_unique($editor_ids);
98
99
100
101

    // Editor plugins' attachments.
    foreach ($editor_ids as $editor_id) {
      $editor = $this->editorManager->createInstance($editor_id);
102
      $attachments[] = $editor->getAttachments();
103
104
    }

105
    // JavaScript settings for Edit.
106
    $definitions = $this->editorManager->getDefinitions();
107
108
109
110
111
112
113
114
115
116
117
    foreach ($definitions as $definition) {
      $attachments[] = array(
        // This will be used in Create.js' propertyEditorWidgetsConfiguration.
        'js' => array(
          array(
            'type' => 'setting',
            'data' => array('edit' => array('editors' => array(
              $definition['id'] => array('widget' => $definition['jsClassName'])
            )))
          )
        )
118
119
120
      );
    }

121
    return NestedArray::mergeDeepArray($attachments);
122
  }
123

124
}