Mercurial > hg > cmmr2012-drupal-site
diff core/modules/media/media.module @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
line wrap: on
line diff
--- a/core/modules/media/media.module Thu Feb 28 11:14:44 2019 +0000 +++ b/core/modules/media/media.module Thu Feb 28 13:11:55 2019 +0000 @@ -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\Template\Attribute; use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; +use Drupal\media\Plugin\media\Source\OEmbedInterface; /** * Implements hook_help(). @@ -72,6 +74,11 @@ 'render element' => 'element', 'base hook' => 'field_multiple_value_form', ], + 'media_oembed_iframe' => [ + 'variables' => [ + 'media' => NULL, + ], + ], ]; } @@ -92,6 +99,7 @@ */ 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 @@ $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 @@ // 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 <a href=":add_page" target="_blank">media add page</a> (opens a new window), then add it by name to the field below.', [':add_page' => $add_url]); } @@ -275,6 +286,10 @@ if ($overview_url->access()) { $elements['#media_help']['#media_list_link'] = t('See the <a href=":list_url" target="_blank">media list</a> (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 @@ } } } + +/** + * 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; +}