dab14ef01f5d3dbe9da42d343e2d481162c24c2f
[yaffs-website] / Editor.php
1 <?php
2
3 namespace Drupal\editor\Plugin\InPlaceEditor;
4
5 use Drupal\Component\Plugin\PluginBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\quickedit\Plugin\InPlaceEditorInterface;
9 use Drupal\filter\Plugin\FilterInterface;
10
11 /**
12  * Defines the formatted text in-place editor.
13  *
14  * @InPlaceEditor(
15  *   id = "editor"
16  * )
17  */
18 class Editor extends PluginBase implements InPlaceEditorInterface {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function isCompatible(FieldItemListInterface $items) {
24     $field_definition = $items->getFieldDefinition();
25
26     // This editor is incompatible with multivalued fields.
27     if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
28       return FALSE;
29     }
30     // This editor is compatible with formatted ("rich") text fields; but only
31     // if there is a currently active text format, that text format has an
32     // associated editor and that editor supports inline editing.
33     elseif ($editor = editor_load($items[0]->format)) {
34       $definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->getEditor());
35       if ($definition['supports_inline_editing'] === TRUE) {
36         return TRUE;
37       }
38     }
39
40     return FALSE;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getMetadata(FieldItemListInterface $items) {
47     $format_id = $items[0]->format;
48     $metadata['format'] = $format_id;
49     $metadata['formatHasTransformations'] = $this->textFormatHasTransformationFilters($format_id);
50     return $metadata;
51   }
52
53   /**
54    * Returns whether the text format has transformation filters.
55    *
56    * @param int $format_id
57    *   A text format ID.
58    *
59    * @return bool
60    */
61   protected function textFormatHasTransformationFilters($format_id) {
62     $format = FilterFormat::load($format_id);
63     return (bool) count(array_intersect([FilterInterface::TYPE_TRANSFORM_REVERSIBLE, FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE], $format->getFiltertypes()));
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getAttachments() {
70     $user = \Drupal::currentUser();
71
72     $user_format_ids = array_keys(filter_formats($user));
73     $manager = \Drupal::service('plugin.manager.editor');
74     $definitions = $manager->getDefinitions();
75
76     // Filter the current user's formats to those that support inline editing.
77     $formats = [];
78     foreach ($user_format_ids as $format_id) {
79       if ($editor = editor_load($format_id)) {
80         $editor_id = $editor->getEditor();
81         if (isset($definitions[$editor_id]['supports_inline_editing']) && $definitions[$editor_id]['supports_inline_editing'] === TRUE) {
82           $formats[] = $format_id;
83         }
84       }
85     }
86
87     // Get the attachments for all text editors that the user might use.
88     $attachments = $manager->getAttachments($formats);
89
90     // Also include editor.module's formatted text editor.
91     $attachments['library'][] = 'editor/quickedit.inPlaceEditor.formattedText';
92
93     return $attachments;
94   }
95
96 }