EditEditorSelector.php

  1. nittany7 modules/contrib/edit/includes/EditEditorSelector.php
  2. mooc7 modules/contrib/edit/includes/EditEditorSelector.php

Contains' Edit's EditorSelector.

See also

Drupal 8's \Drupal\edit\EditorSelector.

Classes

Namesort descending Description
EditEditorSelector Selects an in-place editor (an Editor plugin) for a field.

File

modules/contrib/edit/includes/EditEditorSelector.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains' Edit's EditorSelector.
  5. *
  6. * @see Drupal 8's \Drupal\edit\EditorSelector.
  7. */
  8. module_load_include('php', 'edit', 'includes/EditEditorSelectorInterface');
  9. /**
  10. * Selects an in-place editor (an Editor plugin) for a field.
  11. */
  12. class EditEditorSelector implements EditEditorSelectorInterface {
  13. /**
  14. * A list of alternative editor plugin IDs, keyed by editor plugin ID.
  15. *
  16. * @var array
  17. */
  18. protected $alternatives;
  19. /**
  20. * Constructs a new EditEditorSelector.
  21. */
  22. public function __construct() {
  23. }
  24. /**
  25. * Implements EditEditorSelectorInterface::getEditor().
  26. */
  27. public function getEditor($formatter_type, array $instance, array $items) {
  28. $editors = edit_editor_list();
  29. // Build a static cache of the editors that have registered themselves as
  30. // alternatives to a certain editor.
  31. if (!isset($this->alternatives)) {
  32. foreach ($editors as $alternative_editor_id => $editor) {
  33. if (isset($editor['alternativeTo'])) {
  34. foreach ($editor['alternativeTo'] as $original_editor_id) {
  35. $this->alternatives[$original_editor_id][] = $alternative_editor_id;
  36. }
  37. }
  38. }
  39. }
  40. // Check if the formatter defines an appropriate in-place editor. For
  41. // example, text formatters displaying untrimmed text can choose to use the
  42. // 'direct' editor. If the formatter doesn't specify, fall back to the
  43. // 'form' editor, since that can work for any field. Formatter definitions
  44. // can use 'disabled' to explicitly opt out of in-place editing.
  45. $formatter_settings = $formatter_type['settings'];
  46. $editor_id = (isset($formatter_settings['edit']) && is_array($formatter_settings['edit']) && isset($formatter_settings['edit']['editor'])) ? $formatter_settings['edit']['editor'] : 'form';
  47. if ($editor_id === 'disabled') {
  48. return;
  49. }
  50. elseif ($editor_id === 'form') {
  51. return 'form';
  52. }
  53. // No early return, so create a list of all choices.
  54. $editor_choices = array($editor_id);
  55. if (isset($this->alternatives[$editor_id])) {
  56. $editor_choices = array_merge($editor_choices, $this->alternatives[$editor_id]);
  57. }
  58. // Make a choice.
  59. foreach ($editor_choices as $editor_id) {
  60. $editor = $editors[$editor_id];
  61. if ($editor['file']) {
  62. require_once $editor['file path'] . '/' . $editor['file'];
  63. }
  64. if ($editor['compatibility check callback']($instance, $items)) {
  65. return $editor_id;
  66. }
  67. }
  68. // We still don't have a choice, so fall back to the default 'form' editor.
  69. return 'form';
  70. }
  71. /**
  72. * Implements EditEditorSelectorInterface::getEditorAttachments().
  73. */
  74. public function getEditorAttachments(array $editor_ids, array $metadata) {
  75. $attachments = array();
  76. // Editor plugins' attachments.
  77. foreach (array_unique($editor_ids) as $editor_id) {
  78. $editor = edit_editor_get($editor_id);
  79. if (!empty($editor['attachments callback'])) {
  80. if ($editor['file']) {
  81. require_once $editor['file path'] . '/' . $editor['file'];
  82. }
  83. if (function_exists($editor['attachments callback'])) {
  84. $attachments[$editor_id] = $editor['attachments callback']($metadata);
  85. // Allows contrib to declare additional dependencies for the editor.
  86. drupal_alter('edit_editor_attachments', $attachments[$editor_id], $editor_id, $metadata);
  87. }
  88. }
  89. }
  90. // JavaScript settings for Edit.
  91. foreach (array_unique($editor_ids) as $editor_id) {
  92. $editor = edit_editor_get($editor_id);
  93. $attachments[] = array(
  94. // This will be used in Create.js' propertyEditorWidgetsConfiguration.
  95. 'js' => array(
  96. array(
  97. 'type' => 'setting',
  98. 'data' => array('edit' => array('editors' => array(
  99. $editor_id => array('widget' => $editor['widget'])
  100. )))
  101. )
  102. )
  103. );
  104. }
  105. return drupal_array_merge_deep_array($attachments);
  106. }
  107. }