3 namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Render\Element;
9 use Drupal\entity_browser\WidgetBase;
11 use Drupal\entity_browser\WidgetValidationManager;
12 use Drupal\views\Entity\View as ViewEntity;
13 use Drupal\views\Views;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Drupal\Core\Session\AccountInterface;
16 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
17 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18 use Drupal\Core\Entity\EntityTypeManagerInterface;
21 * Uses a view to provide entity listing in a browser's widget.
23 * @EntityBrowserWidget(
25 * label = @Translation("View"),
27 * description = @Translation("Uses a view to provide entity listing in a browser's widget."),
31 class View extends WidgetBase implements ContainerFactoryPluginInterface {
36 * @var \Drupal\Core\Session\AccountInterface
38 protected $currentUser;
43 public function defaultConfiguration() {
46 'view_display' => NULL,
47 ] + parent::defaultConfiguration();
53 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
58 $container->get('event_dispatcher'),
59 $container->get('entity_type.manager'),
60 $container->get('plugin.manager.entity_browser.widget_validation'),
61 $container->get('current_user')
66 * Constructs a new View object.
68 * @param array $configuration
69 * A configuration array containing information about the plugin instance.
70 * @param string $plugin_id
71 * The plugin_id for the plugin instance.
72 * @param mixed $plugin_definition
73 * The plugin implementation definition.
74 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
75 * Event dispatcher service.
76 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
77 * The entity type manager.
78 * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager
79 * The Widget Validation Manager service.
80 * @param \Drupal\Core\Session\AccountInterface $current_user
83 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, AccountInterface $current_user) {
84 parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
85 $this->currentUser = $current_user;
91 public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
92 $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
93 // TODO - do we need better error handling for view and view_display (in case
94 // either of those is nonexistent or display not of correct type)?
95 $form['#attached']['library'] = ['entity_browser/view'];
97 /** @var \Drupal\views\ViewExecutable $view */
98 $view = $this->entityTypeManager
100 ->load($this->configuration['view'])
103 // Check if the current user has access to this view.
104 if (!$view->access($this->configuration['view_display'])) {
106 '#markup' => $this->t('You do not have access to this View.'),
110 if (!empty($this->configuration['arguments'])) {
111 if (!empty($additional_widget_parameters['path_parts'])) {
113 // Map configuration arguments with original path parts.
114 foreach ($this->configuration['arguments'] as $argument) {
115 $arguments[] = isset($additional_widget_parameters['path_parts'][$argument]) ? $additional_widget_parameters['path_parts'][$argument] : '';
117 $view->setArguments(array_values($arguments));
121 $form['view'] = $view->executeDisplay($this->configuration['view_display']);
123 if (empty($view->field['entity_browser_select'])) {
124 $url = Url::fromRoute('entity.view.edit_form', ['view' => $this->configuration['view']])->toString();
125 if ($this->currentUser->hasPermission('administer views')) {
127 '#markup' => $this->t('Entity browser select form field not found on a view. <a href=":link">Go fix it</a>!', [':link' => $url]),
132 '#markup' => $this->t('Entity browser select form field not found on a view. Go fix it!'),
137 // When rebuilding makes no sense to keep checkboxes that were previously
139 if (!empty($form['view']['entity_browser_select'])) {
140 foreach (Element::children($form['view']['entity_browser_select']) as $child) {
141 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\entity_browser\Plugin\EntityBrowser\Widget\View', 'processCheckbox'];
142 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processAjaxForm'];
143 $form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processGroup'];
147 $form['view']['view'] = [
148 '#markup' => \Drupal::service('renderer')->render($form['view']['view']),
155 * Sets the #checked property when rebuilding form.
157 * Every time when we rebuild we want all checkboxes to be unchecked.
159 * @see \Drupal\Core\Render\Element\Checkbox::processCheckbox()
161 public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
162 if ($form_state->isRebuilding()) {
163 $element['#checked'] = FALSE;
172 public function validate(array &$form, FormStateInterface $form_state) {
173 $user_input = $form_state->getUserInput();
174 if (isset($user_input['entity_browser_select'])) {
175 $selected_rows = array_values(array_filter($user_input['entity_browser_select']));
176 foreach ($selected_rows as $row) {
177 // Verify that the user input is a string and split it.
178 // Each $row is in the format entity_type:id.
179 if (is_string($row) && $parts = explode(':', $row, 2)) {
180 // Make sure we have a type and id present.
181 if (count($parts) == 2) {
183 $storage = $this->entityTypeManager->getStorage($parts[0]);
184 if (!$storage->load($parts[1])) {
185 $message = $this->t('The @type Entity @id does not exist.', [
186 '@type' => $parts[0],
189 $form_state->setError($form['widget']['view']['entity_browser_select'], $message);
192 catch (PluginNotFoundException $e) {
193 $message = $this->t('The Entity Type @type does not exist.', [
194 '@type' => $parts[0],
196 $form_state->setError($form['widget']['view']['entity_browser_select'], $message);
202 // If there weren't any errors set, run the normal validators.
203 if (empty($form_state->getErrors())) {
204 parent::validate($form, $form_state);
212 protected function prepareEntities(array $form, FormStateInterface $form_state) {
213 $selected_rows = array_values(array_filter($form_state->getUserInput()['entity_browser_select']));
215 foreach ($selected_rows as $row) {
216 list($type, $id) = explode(':', $row);
217 $storage = $this->entityTypeManager->getStorage($type);
218 if ($entity = $storage->load($id)) {
219 $entities[] = $entity;
228 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
229 $entities = $this->prepareEntities($form, $form_state);
230 $this->selectEntities($entities, $form_state);
236 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
237 $form = parent::buildConfigurationForm($form, $form_state);
240 // Get only those enabled Views that have entity_browser displays.
241 $displays = Views::getApplicableViews('entity_browser_display');
242 foreach ($displays as $display) {
243 list($view_id, $display_id) = $display;
244 $view = $this->entityTypeManager->getStorage('view')->load($view_id);
245 $options[$view_id . '.' . $display_id] = $this->t('@view : @display', ['@view' => $view->label(), '@display' => $view->get('display')[$display_id]['display_title']]);
250 '#title' => $this->t('View : View display'),
251 '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
252 '#options' => $options,
253 '#empty_option' => $this->t('- Select a view -'),
263 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
264 $values = $form_state->getValues()['table'][$this->uuid()]['form'];
265 $this->configuration['submit_text'] = $values['submit_text'];
266 $this->configuration['auto_select'] = $values['auto_select'];
267 if (!empty($values['view'])) {
268 list($view_id, $display_id) = explode('.', $values['view']);
269 $this->configuration['view'] = $view_id;
270 $this->configuration['view_display'] = $display_id;
277 public function calculateDependencies() {
279 if ($this->configuration['view']) {
280 $view = ViewEntity::load($this->configuration['view']);
281 $dependencies[$view->getConfigDependencyKey()] = [$view->getConfigDependencyName()];
283 return $dependencies;
289 public function access() {
290 // Mark the widget as not visible if the user has no access to the view.
291 /** @var \Drupal\views\ViewExecutable $view */
292 $view = $this->entityTypeManager
294 ->load($this->configuration['view'])
298 // Check if the current user has access to this view.
299 return AccessResult::allowedIf($view->access($this->configuration['view_display']));