3 namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8 use Drupal\entity_browser\WidgetBase;
10 use Drupal\entity_browser\WidgetValidationManager;
11 use Drupal\views\Entity\View as ViewEntity;
12 use Drupal\views\Views;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Drupal\Core\Session\AccountInterface;
15 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
20 * Uses a view to provide entity listing in a browser's widget.
22 * @EntityBrowserWidget(
24 * label = @Translation("View"),
26 * description = @Translation("Uses a view to provide entity listing in a browser's widget."),
30 class View extends WidgetBase implements ContainerFactoryPluginInterface {
35 * @var \Drupal\Core\Session\AccountInterface
37 protected $currentUser;
42 public function defaultConfiguration() {
45 'view_display' => NULL,
46 ] + parent::defaultConfiguration();
52 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57 $container->get('event_dispatcher'),
58 $container->get('entity_type.manager'),
59 $container->get('plugin.manager.entity_browser.widget_validation'),
60 $container->get('current_user')
65 * Constructs a new View object.
67 * @param array $configuration
68 * A configuration array containing information about the plugin instance.
69 * @param string $plugin_id
70 * The plugin_id for the plugin instance.
71 * @param mixed $plugin_definition
72 * The plugin implementation definition.
73 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
74 * Event dispatcher service.
75 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
76 * The entity type manager.
77 * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager
78 * The Widget Validation Manager service.
79 * @param \Drupal\Core\Session\AccountInterface $current_user
82 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, AccountInterface $current_user) {
83 parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
84 $this->currentUser = $current_user;
90 public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
91 $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
92 // TODO - do we need better error handling for view and view_display (in case
93 // either of those is nonexistent or display not of correct type)?
94 $form['#attached']['library'] = ['entity_browser/view'];
96 /** @var \Drupal\views\ViewExecutable $view */
97 $view = $this->entityTypeManager
99 ->load($this->configuration['view'])
102 // Check if the current user has access to this view.
103 if (!$view->access($this->configuration['view_display'])) {
105 '#markup' => $this->t('You do not have access to this View.'),
109 if (!empty($this->configuration['arguments'])) {
110 if (!empty($additional_widget_parameters['path_parts'])) {
112 // Map configuration arguments with original path parts.
113 foreach ($this->configuration['arguments'] as $argument) {
114 $arguments[] = isset($additional_widget_parameters['path_parts'][$argument]) ? $additional_widget_parameters['path_parts'][$argument] : '';
116 $view->setArguments(array_values($arguments));
120 $form['view'] = $view->executeDisplay($this->configuration['view_display']);
122 if (empty($view->field['entity_browser_select'])) {
123 $url = Url::fromRoute('entity.view.edit_form', ['view' => $this->configuration['view']])->toString();
124 if ($this->currentUser->hasPermission('administer views')) {
126 '#markup' => $this->t('Entity browser select form field not found on a view. <a href=":link">Go fix it</a>!', [':link' => $url]),
131 '#markup' => $this->t('Entity browser select form field not found on a view. Go fix it!'),
136 // When rebuilding makes no sense to keep checkboxes that were previously
138 if (!empty($form['view']['entity_browser_select']) && $form_state->isRebuilding()) {
139 foreach (Element::children($form['view']['entity_browser_select']) as $child) {
140 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\entity_browser\Plugin\EntityBrowser\Widget\View', 'processCheckbox'];
141 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processAjaxForm'];
142 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processGroup'];
146 $form['view']['view'] = [
147 '#markup' => \Drupal::service('renderer')->render($form['view']['view']),
154 * Sets the #checked property when rebuilding form.
156 * Every time when we rebuild we want all checkboxes to be unchecked.
158 * @see \Drupal\Core\Render\Element\Checkbox::processCheckbox()
160 public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
161 $element['#checked'] = FALSE;
168 public function validate(array &$form, FormStateInterface $form_state) {
169 $user_input = $form_state->getUserInput();
170 if (isset($user_input['entity_browser_select'])) {
171 $selected_rows = array_values(array_filter($user_input['entity_browser_select']));
172 foreach ($selected_rows as $row) {
173 // Verify that the user input is a string and split it.
174 // Each $row is in the format entity_type:id.
175 if (is_string($row) && $parts = explode(':', $row, 2)) {
176 // Make sure we have a type and id present.
177 if (count($parts) == 2) {
179 $storage = $this->entityTypeManager->getStorage($parts[0]);
180 if (!$storage->load($parts[1])) {
181 $message = $this->t('The @type Entity @id does not exist.', [
182 '@type' => $parts[0],
185 $form_state->setError($form['widget']['view']['entity_browser_select'], $message);
188 catch (PluginNotFoundException $e) {
189 $message = $this->t('The Entity Type @type does not exist.', [
190 '@type' => $parts[0],
192 $form_state->setError($form['widget']['view']['entity_browser_select'], $message);
198 // If there weren't any errors set, run the normal validators.
199 if (empty($form_state->getErrors())) {
200 parent::validate($form, $form_state);
208 protected function prepareEntities(array $form, FormStateInterface $form_state) {
209 $selected_rows = array_values(array_filter($form_state->getUserInput()['entity_browser_select']));
211 foreach ($selected_rows as $row) {
212 list($type, $id) = explode(':', $row);
213 $storage = $this->entityTypeManager->getStorage($type);
214 if ($entity = $storage->load($id)) {
215 $entities[] = $entity;
224 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
225 $entities = $this->prepareEntities($form, $form_state);
226 $this->selectEntities($entities, $form_state);
232 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
233 $form = parent::buildConfigurationForm($form, $form_state);
236 // Get only those enabled Views that have entity_browser displays.
237 $displays = Views::getApplicableViews('entity_browser_display');
238 foreach ($displays as $display) {
239 list($view_id, $display_id) = $display;
240 $view = $this->entityTypeManager->getStorage('view')->load($view_id);
241 $options[$view_id . '.' . $display_id] = $this->t('@view : @display', ['@view' => $view->label(), '@display' => $view->get('display')[$display_id]['display_title']]);
246 '#title' => $this->t('View : View display'),
247 '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
248 '#options' => $options,
249 '#empty_option' => $this->t('- Select a view -'),
259 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
260 $values = $form_state->getValues()['table'][$this->uuid()]['form'];
261 $this->configuration['submit_text'] = $values['submit_text'];
262 $this->configuration['auto_select'] = $values['auto_select'];
263 if (!empty($values['view'])) {
264 list($view_id, $display_id) = explode('.', $values['view']);
265 $this->configuration['view'] = $view_id;
266 $this->configuration['view_display'] = $display_id;
273 public function calculateDependencies() {
275 if ($this->configuration['view']) {
276 $view = ViewEntity::load($this->configuration['view']);
277 $dependencies[$view->getConfigDependencyKey()] = [$view->getConfigDependencyName()];
279 return $dependencies;