You are here

function _media_get_add_url in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/media.module \_media_get_add_url()

Returns the appropriate URL to add media for the current user.

@todo Remove in https://www.drupal.org/project/drupal/issues/2938116

@internal This function is internal and may be removed in a minor release.

Parameters

string[] $allowed_bundles: An array of bundles that should be checked for create access.

Return value

bool|\Drupal\Core\Url The URL to add media, or FALSE if the user cannot create any media.

1 call to _media_get_add_url()
media_field_widget_multivalue_form_alter in core/modules/media/media.module
Implements hook_field_widget_multivalue_form_alter().

File

core/modules/media/media.module, line 344
Provides media items.

Code

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;
}