View source
<?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_directories_ui\MediaDirectoriesLibraryState;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class DirectoryBrowserUiForm extends FormBase {
protected $currentUser;
protected $entityTypeManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('event_dispatcher'), $container
->get('entity_type.manager'), $container
->get('current_user'));
}
public function __construct(EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}
public function buildForm(array $form, FormStateInterface $form_state, MediaDirectoriesLibraryState $state = NULL) {
$form['#attached']['drupalSettings']['media_directories']['cardinality'] = -1;
$form['#attached']['drupalSettings']['media_directories']['target_bundles'] = [];
$form['browser'] = [
'#theme' => 'media_directories_browser',
];
$form['browser']['active_directory'] = [
'#type' => 'hidden',
'#default_value' => MEDIA_DIRECTORY_ROOT,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Insert selected'),
];
return $form;
}
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'];
}
public function getFormId() {
return 'directory_browser_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}