View source
<?php
namespace Drupal\media_directories_ui\Plugin\EntityBrowser\Widget;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\entity_browser\WidgetBase;
use Drupal\Core\Url;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\entity_browser\WidgetValidationManager;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
class DirectoryBrowser extends WidgetBase implements ContainerFactoryPluginInterface {
protected $currentUser;
protected $routeMatch;
protected $moduleHandler;
protected $vocabularyId;
public function defaultConfiguration() {
return [] + parent::defaultConfiguration();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('event_dispatcher'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.entity_browser.widget_validation'), $container
->get('current_user'), $container
->get('current_route_match'), $container
->get('module_handler'), $container
->get('config.factory'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, AccountInterface $current_user, RouteMatchInterface $route_match, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
$this->currentUser = $current_user;
$this->routeMatch = $route_match;
$this->moduleHandler = $module_handler;
$config = $config_factory
->get('media_directories.settings');
$this->vocabularyId = $config
->get('directory_taxonomy');
}
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';
$form['#attached']['drupalSettings']['media_directories']['cardinality'] = -1;
$form['#attached']['drupalSettings']['media_directories']['target_bundles'] = [];
$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(),
];
$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;
$route_parameter_entity_browser_id = $this->routeMatch
->getParameter('entity_browser_id');
$selection_mode = 'reset';
if ($route_parameter_entity_browser_id == 'media_directories_overview') {
$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') {
$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 {
$types = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
foreach ($types as $type) {
$enabled_bundles[] = $type
->id();
}
}
$form['#attached']['drupalSettings']['media_directories']['target_bundles'] = $enabled_bundles;
foreach ($enabled_bundles as $type) {
$media_translation_enabled = FALSE;
if ($this->moduleHandler
->moduleExists('content_translation')) {
$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']);
}
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;
}
public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
if ($form_state
->isRebuilding()) {
$element['#checked'] = FALSE;
}
return $element;
}
public function validate(array &$form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
if (isset($user_input['entity_browser_select'])) {
$selected_rows = array_values(array_filter($user_input['entity_browser_select']));
foreach ($selected_rows as $row) {
if (is_string($row) && ($parts = explode(':', $row, 2))) {
if (count($parts) == 2) {
try {
$storage = $this->entityTypeManager
->getStorage($parts[0]);
if (!$storage
->load($parts[1])) {
$message = $this
->t('The @type Entity @id does not exist.', [
'@type' => $parts[0],
'@id' => $parts[1],
]);
$form_state
->setError($form['widget']['view']['entity_browser_select'], $message);
}
} catch (PluginNotFoundException $e) {
$message = $this
->t('The Entity Type @type does not exist.', [
'@type' => $parts[0],
]);
$form_state
->setError($form['widget']['view']['entity_browser_select'], $message);
}
}
}
}
if (empty($form_state
->getErrors())) {
parent::validate($form, $form_state);
}
}
else {
$form_state
->setError($form, $this
->t('Please select media element!'));
}
}
protected function prepareEntities(array $form, FormStateInterface $form_state) {
$selected_rows = array_values(array_filter($form_state
->getUserInput()['entity_browser_select']));
$entities = [];
foreach ($selected_rows as $row) {
list($type, $id) = explode(':', $row);
$storage = $this->entityTypeManager
->getStorage($type);
if ($entity = $storage
->load($id)) {
$entities[] = $entity;
}
}
return $entities;
}
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$entities = $this
->prepareEntities($form, $form_state);
$this
->selectEntities($entities, $form_state);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues()['table'][$this
->uuid()]['form'];
$this->configuration['submit_text'] = $values['submit_text'];
$this->configuration['auto_select'] = $values['auto_select'];
}
}