You are here

public function DirectoryBrowser::getForm in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Plugin/EntityBrowser/Widget/DirectoryBrowser.php \Drupal\media_directories_ui\Plugin\EntityBrowser\Widget\DirectoryBrowser::getForm()
  2. 2.x modules/media_directories_ui/src/Plugin/EntityBrowser/Widget/DirectoryBrowser.php \Drupal\media_directories_ui\Plugin\EntityBrowser\Widget\DirectoryBrowser::getForm()

File

modules/media_directories_ui/src/Plugin/EntityBrowser/Widget/DirectoryBrowser.php, line 125

Class

DirectoryBrowser
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\media_directories_ui\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
  $form['#attached']['library'][] = 'media_directories_ui/media-ui';

  // Default values.
  $form['#attached']['drupalSettings']['media_directories']['cardinality'] = -1;
  $form['#attached']['drupalSettings']['media_directories']['target_bundles'] = [];

  // Routes for different actions.
  $form['#attached']['drupalSettings']['media_directories']['url'] = [
    'directory.tree' => Url::fromRoute('media_directories_ui.directory.tree')
      ->toString(),
    'directory.content' => Url::fromRoute('media_directories_ui.directory.content')
      ->toString(),
    'directory.add' => Url::fromRoute('media_directories_ui.directory.add')
      ->toString(),
    'directory.rename' => Url::fromRoute('media_directories_ui.directory.rename')
      ->toString(),
    'directory.delete' => Url::fromRoute('media_directories_ui.directory.delete')
      ->toString(),
    'directory.move' => Url::fromRoute('media_directories_ui.directory.move')
      ->toString(),
    'media.add' => Url::fromRoute('media_directories_ui.media.add')
      ->toString(),
    'media.edit' => Url::fromRoute('media_directories_ui.media.edit')
      ->toString(),
    'media.move' => Url::fromRoute('media_directories_ui.media.move')
      ->toString(),
    'media.delete' => Url::fromRoute('media_directories_ui.media.delete')
      ->toString(),
  ];

  // Pass taxonomy permissions.
  $vocabulary_permissions = [
    'create' => FALSE,
    'update' => FALSE,
    'translate' => FALSE,
    'delete' => FALSE,
  ];
  if (isset($this->vocabularyId)) {
    $directory = Term::create([
      'name' => 'will not be saved',
      'vid' => $this->vocabularyId,
    ]);
    $vocabulary_permissions['create'] = $directory
      ->access('create');
    $vocabulary_permissions['update'] = $directory
      ->access('update');
    $vocabulary_permissions['translate'] = $directory
      ->access('translate');
    $vocabulary_permissions['delete'] = $directory
      ->access('delete');
  }
  $form['#attached']['drupalSettings']['media_directories']['vocabulary_permissions'] = $vocabulary_permissions;

  // Decide the selection mode.
  $route_parameter_entity_browser_id = $this->routeMatch
    ->getParameter('entity_browser_id');
  $selection_mode = 'reset';
  if ($route_parameter_entity_browser_id == 'media_directories_overview') {

    // We are on the media overview page.
    $selection_mode = 'keep';
  }
  $form['#attached']['drupalSettings']['media_directories']['selection_mode'] = $selection_mode;
  $cardinality = (int) NestedArray::getValue($form_state
    ->getStorage(), [
    'entity_browser',
    'validators',
    'cardinality',
    'cardinality',
  ]);
  $remaining = (int) NestedArray::getValue($form_state
    ->getStorage(), [
    'entity_browser',
    'widget_context',
    'remaining',
  ]);
  if ($route_parameter_entity_browser_id == 'media_directories_editor_browser') {

    // Allow only one item to be selected in the editor.
    $remaining = 1;
  }
  $target_bundles = NestedArray::getValue($form_state
    ->getStorage(), [
    'entity_browser',
    'validators',
    'target_bundles',
  ]);
  if ($cardinality) {
    $form['#attached']['drupalSettings']['media_directories']['cardinality'] = $cardinality;
  }
  if ($remaining) {
    $form['#attached']['drupalSettings']['media_directories']['remaining'] = $remaining;
  }
  $enabled_bundles = [];
  if ($target_bundles) {
    $enabled_bundles = $target_bundles['bundle'];
  }
  else {

    /** @var \Drupal\media\Entity\MediaType[] $types */
    $types = $this->entityTypeManager
      ->getStorage('media_type')
      ->loadMultiple();
    foreach ($types as $type) {
      $enabled_bundles[] = $type
        ->id();
    }
  }
  $form['#attached']['drupalSettings']['media_directories']['target_bundles'] = $enabled_bundles;

  // Check wheater medias are translatable and pass on as javascript settings.
  foreach ($enabled_bundles as $type) {
    $media_translation_enabled = FALSE;
    if ($this->moduleHandler
      ->moduleExists('content_translation')) {

      // I found no way to inject an optional service into this plugin.
      // @see https://symfony.com/doc/current/service_container/optional_dependencies.html
      // @see https://orkjern.com/services-with-optional-dependencies-drupal-8
      // @see https://www.md-systems.ch/en/blog/techblog/2016/12/17/how-to-safely-inject-additional-services-into-an-overridden-service
      $media_translation_enabled = \Drupal::service('content_translation.manager')
        ->isEnabled('media', $type);
    }
    $form['#attached']['drupalSettings']['media_directories']['media_translation_enabled'][$type] = $media_translation_enabled;
  }
  $term_translation_enabled = FALSE;
  if ($this->moduleHandler
    ->moduleExists('content_translation')) {
    $term_translation_enabled = \Drupal::service('content_translation.manager')
      ->isEnabled('taxonomy_term', $this->vocabularyId);
  }
  $form['#attached']['drupalSettings']['media_directories']['term_translation_enabled'] = $term_translation_enabled;
  $form['browser'] = [
    '#theme' => 'media_directories_browser',
  ];
  $form['browser']['active_directory'] = [
    '#type' => 'hidden',
    '#default_value' => MEDIA_DIRECTORY_ROOT,
  ];
  if ($this->configuration['entity_browser_id'] === 'media_directories_overview') {
    $form['actions']['#access'] = FALSE;
    $form['browser']['#attributes']['class'][] = 'media-browser--full';
  }
  else {
    $form['browser']['actions'] = $form['actions'];
    unset($form['actions']);
  }

  // When rebuilding makes no sense to keep checkboxes that were previously
  // selected.
  if (!empty($form['browser']['entity_browser_select'])) {
    foreach (Element::children($form['browser']['entity_browser_select']) as $child) {
      $form['browser']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\entity_browser\\Plugin\\EntityBrowser\\Widget\\View',
        'processCheckbox',
      ];
      $form['browser']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\Core\\Render\\Element\\Checkbox',
        'processAjaxForm',
      ];
      $form['browser']['entity_browser_select'][$child]['#process'][] = [
        '\\Drupal\\Core\\Render\\Element\\Checkbox',
        'processGroup',
      ];
    }
  }
  return $form;
}