+
+/**
+ * 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;
+}