summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/media/media.module
blob: 537cbee18620dd25a157a9761985bad2bdccbfc6 (plain) (blame)
1
2
3
4
5
6
7
8
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

/**
 * @file
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\StopProceduralHookScan;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElementBase;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;

/**
 * Prepares variables for media templates.
 *
 * Default template: media.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An array of elements to display in view mode.
 *   - media: The media item.
 *   - name: The label for the media item.
 *   - view_mode: View mode; e.g., 'full', 'teaser', etc.
 */
#[StopProceduralHookScan]
function template_preprocess_media(array &$variables): void {
  $variables['media'] = $variables['elements']['#media'];
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['name'] = $variables['media']->label();

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}

/**
 * Implements hook_preprocess_HOOK() for media reference widgets.
 */
function media_preprocess_media_reference_help(&$variables): void {
  // Most of these attribute checks are copied from
  // template_preprocess_fieldset(). Our template extends
  // field-multiple-value-form.html.twig to provide our help text, but also
  // groups the information within a semantic fieldset with a legend. So, we
  // incorporate parity for both.
  $element = $variables['element'];
  Element::setAttributes($element, ['id']);
  RenderElementBase::setAttributes($element);
  $variables['attributes'] = $element['#attributes'] ?? [];
  $variables['legend_attributes'] = new Attribute();
  $variables['header_attributes'] = new Attribute();
  $variables['description']['attributes'] = new Attribute();
  $variables['legend_span_attributes'] = new Attribute();

  if (!empty($element['#media_help'])) {
    foreach ($element['#media_help'] as $key => $text) {
      $variables[substr($key, 1)] = $text;
    }
  }
}

/**
 * Returns the appropriate URL to add media for the current user.
 *
 * @todo Remove in https://www.drupal.org/project/drupal/issues/2938116
 *
 * @param string[] $allowed_bundles
 *   An array of bundles that should be checked for create access.
 *
 * @return bool|\Drupal\Core\Url
 *   The URL to add media, or FALSE if the user cannot create any media.
 *
 * @internal
 *   This function is internal and may be removed in a minor release.
 */
function _media_get_add_url($allowed_bundles) {
  $access_handler = \Drupal::entityTypeManager()->getAccessControlHandler('media');
  $create_bundles = array_filter($allowed_bundles, [$access_handler, 'createAccess']);

  // Add a section about how to create media if the user has access to do so.
  if (count($create_bundles) === 1) {
    return Url::fromRoute('entity.media.add_form', ['media_type' => reset($create_bundles)])->toString();
  }
  elseif (count($create_bundles) > 1) {
    return Url::fromRoute('entity.media.add_page')->toString();
  }

  return FALSE;
}

/**
 * Validate callback to ensure filter order and allowed_html are compatible.
 */
function media_filter_format_edit_form_validate($form, FormStateInterface $form_state): void {
  if ($form_state->getTriggeringElement()['#name'] !== 'op') {
    return;
  }

  $allowed_html_path = [
    'filters',
    'filter_html',
    'settings',
    'allowed_html',
  ];

  $filter_html_settings_path = [
    'filters',
    'filter_html',
    'settings',
  ];

  $filter_html_enabled = $form_state->getValue([
    'filters',
    'filter_html',
    'status',
  ]);

  $media_embed_enabled = $form_state->getValue([
    'filters',
    'media_embed',
    'status',
  ]);

  if (!$media_embed_enabled) {
    return;
  }

  $get_filter_label = function ($filter_plugin_id) use ($form) {
    return (string) $form['filters']['order'][$filter_plugin_id]['filter']['#markup'];
  };

  if ($filter_html_enabled && $form_state->getValue($allowed_html_path)) {
    /** @var \Drupal\filter\Entity\FilterFormat $filter_format */
    $filter_format = $form_state->getFormObject()->getEntity();

    $filter_html = clone $filter_format->filters()->get('filter_html');
    $filter_html->setConfiguration(['settings' => $form_state->getValue($filter_html_settings_path)]);
    $restrictions = $filter_html->getHTMLRestrictions();
    $allowed = $restrictions['allowed'];

    // Require `<drupal-media>` HTML tag if filter_html is enabled.
    if (!isset($allowed['drupal-media'])) {
      $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The %media-embed-filter-label filter requires <code>&lt;drupal-media&gt;</code> among the allowed HTML tags.', [
        '%media-embed-filter-label' => $get_filter_label('media_embed'),
      ]));
    }
    else {
      $required_attributes = [
        'data-entity-type',
        'data-entity-uuid',
      ];

      // If there are no attributes, the allowed item is set to FALSE,
      // otherwise, it is set to an array.
      if ($allowed['drupal-media'] === FALSE) {
        $missing_attributes = $required_attributes;
      }
      else {
        $missing_attributes = array_diff($required_attributes, array_keys($allowed['drupal-media']));
      }

      if ($missing_attributes) {
        $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The <code>&lt;drupal-media&gt;</code> tag in the allowed HTML tags is missing the following attributes: <code>%list</code>.', [
          '%list' => implode(', ', $missing_attributes),
        ]));
      }
    }
  }

  $filters = $form_state->getValue('filters');

  // The "media_embed" filter must run after "filter_align", "filter_caption",
  // and "filter_html_image_secure".
  $precedents = [
    'filter_align',
    'filter_caption',
    'filter_html_image_secure',
  ];

  $error_filters = [];
  foreach ($precedents as $filter_name) {
    // A filter that should run before media embed filter.
    $precedent = $filters[$filter_name];

    if (empty($precedent['status']) || !isset($precedent['weight'])) {
      continue;
    }

    if ($precedent['weight'] >= $filters['media_embed']['weight']) {
      $error_filters[$filter_name] = $get_filter_label($filter_name);
    }
  }

  if (!empty($error_filters)) {
    $error_message = \Drupal::translation()->formatPlural(
      count($error_filters),
      'The %media-embed-filter-label filter needs to be placed after the %filter filter.',
      'The %media-embed-filter-label filter needs to be placed after the following filters: %filters.',
      [
        '%media-embed-filter-label' => $get_filter_label('media_embed'),
        '%filter' => reset($error_filters),
        '%filters' => implode(', ', $error_filters),
      ]
    );

    $form_state->setErrorByName('filters', $error_message);
  }
}