namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
+use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\entity_browser\WidgetBase;
// When rebuilding makes no sense to keep checkboxes that were previously
// selected.
- if (!empty($form['view']['entity_browser_select']) && $form_state->isRebuilding()) {
+ if (!empty($form['view']['entity_browser_select'])) {
foreach (Element::children($form['view']['entity_browser_select']) as $child) {
$form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\entity_browser\Plugin\EntityBrowser\Widget\View', 'processCheckbox'];
$form['view']['entity_browser_select'][$child]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processAjaxForm'];
* @see \Drupal\Core\Render\Element\Checkbox::processCheckbox()
*/
public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
- $element['#checked'] = FALSE;
+ if ($form_state->isRebuilding()) {
+ $element['#checked'] = FALSE;
+ }
+
return $element;
}
return $dependencies;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function access() {
+ // Mark the widget as not visible if the user has no access to the view.
+ /** @var \Drupal\views\ViewExecutable $view */
+ $view = $this->entityTypeManager
+ ->getStorage('view')
+ ->load($this->configuration['view'])
+ ->getExecutable();
+
+
+ // Check if the current user has access to this view.
+ return AccessResult::allowedIf($view->access($this->configuration['view_display']));
+ }
+
}