function _media_get_add_url

Same name and namespace in other branches
  1. 8.9.x core/modules/media/media.module \_media_get_add_url()
  2. 10 core/modules/media/media.module \_media_get_add_url()
  3. 11.x 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_complete_form_alter in core/modules/media/media.module
Implements hook_field_widget_complete_form_alter().

File

core/modules/media/media.module, line 346

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.