X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Fmedia.module;fp=web%2Fcore%2Fmodules%2Fmedia%2Fmedia.module;h=a95b972d66565a9af43f446fb5de719c898db858;hp=5079f0fa9a868bb9a76bbdcf4ecd19fd2d9e6b51;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/media/media.module b/web/core/modules/media/media.module index 5079f0fa9..a95b972d6 100644 --- a/web/core/modules/media/media.module +++ b/web/core/modules/media/media.module @@ -5,6 +5,7 @@ * Provides media items. */ +use Drupal\Component\Plugin\DerivativeInspectionInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; @@ -15,6 +16,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Template\Attribute; use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; +use Drupal\media\Plugin\media\Source\OEmbedInterface; /** * Implements hook_help(). @@ -72,6 +74,11 @@ function media_theme() { 'render element' => 'element', 'base hook' => 'field_multiple_value_form', ], + 'media_oembed_iframe' => [ + 'variables' => [ + 'media' => NULL, + ], + ], ]; } @@ -92,6 +99,7 @@ function media_entity_access(EntityInterface $entity, $operation, AccountInterfa */ function media_theme_suggestions_media(array $variables) { $suggestions = []; + /** @var \Drupal\media\MediaInterface $media */ $media = $variables['elements']['#media']; $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); @@ -99,6 +107,31 @@ function media_theme_suggestions_media(array $variables) { $suggestions[] = 'media__' . $media->bundle(); $suggestions[] = 'media__' . $media->bundle() . '__' . $sanitized_view_mode; + // Add suggestions based on the source plugin ID. + $source = $media->getSource(); + if ($source instanceof DerivativeInspectionInterface) { + $source_id = $source->getBaseId(); + $derivative_id = $source->getDerivativeId(); + if ($derivative_id) { + $source_id .= '__derivative_' . $derivative_id; + } + } + else { + $source_id = $source->getPluginId(); + } + $suggestions[] = "media__source_$source_id"; + + // If the source plugin uses oEmbed, add a suggestion based on the provider + // name, if available. + if ($source instanceof OEmbedInterface) { + $provider_id = $source->getMetadata($media, 'provider_name'); + if ($provider_id) { + $provider_id = \Drupal::transliteration()->transliterate($provider_id); + $provider_id = preg_replace('/[^a-z0-9_]+/', '_', mb_strtolower($provider_id)); + $suggestions[] = end($suggestions) . "__provider_$provider_id"; + } + } + return $suggestions; } @@ -210,31 +243,9 @@ function media_field_widget_multivalue_form_alter(array &$elements, FormStateInt // Retrieve the media bundle list and add information for the user based on // which bundles are available to be created or referenced. $settings = $context['items']->getFieldDefinition()->getSetting('handler_settings'); - $allowed_bundles = isset($settings['target_bundles']) ? $settings['target_bundles'] : []; - $access_handler = \Drupal::entityTypeManager()->getAccessControlHandler('media'); - $all_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media'); - $bundle_labels = []; - $create_bundles = []; - foreach ($allowed_bundles as $bundle) { - $bundle_labels[] = $all_bundles[$bundle]['label']; - if ($access_handler->createAccess($bundle)) { - $create_bundles[] = $bundle; - if (count($create_bundles) > 1) { - // If the user has access to create more than 1 bundle then the - // individual media type form can not be used. - break; - } - } - } - - // Add a section about how to create media if the user has access to do so. - if (!empty($create_bundles)) { - if (count($create_bundles) === 1) { - $add_url = Url::fromRoute('entity.media.add_form', ['media_type' => $create_bundles[0]])->toString(); - } - elseif (count($create_bundles) > 1) { - $add_url = Url::fromRoute('entity.media.add_page')->toString(); - } + $allowed_bundles = !empty($settings['target_bundles']) ? $settings['target_bundles'] : []; + $add_url = _media_get_add_url($allowed_bundles); + if ($add_url) { $elements['#media_help']['#media_add_help'] = t('Create your media on the media add page (opens a new window), then add it by name to the field below.', [':add_page' => $add_url]); } @@ -275,6 +286,10 @@ function media_field_widget_multivalue_form_alter(array &$elements, FormStateInt if ($overview_url->access()) { $elements['#media_help']['#media_list_link'] = t('See the media list (opens a new window) to help locate media.', [':list_url' => $overview_url->toString()]); } + $all_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media'); + $bundle_labels = array_map(function ($bundle) use ($all_bundles) { + return $all_bundles[$bundle]['label']; + }, $allowed_bundles); $elements['#media_help']['#allowed_types_help'] = t('Allowed media types: %types', ['%types' => implode(", ", $bundle_labels)]); } } @@ -303,3 +318,32 @@ function media_preprocess_media_reference_help(&$variables) { } } } + +/** + * 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; +}