diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php index 8b53b38..e4f14c0 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php @@ -35,6 +35,13 @@ protected $column; /** + * Current user object. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * {@inheritdoc} */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings) { @@ -114,10 +121,7 @@ public static function validateElement(array $element, FormStateInterface $form_ protected function getOptions(FieldableEntityInterface $entity) { if (!isset($this->options)) { // Limit the settable options for the current user account. - $options = $this->fieldDefinition - ->getFieldStorageDefinition() - ->getOptionsProvider($this->column, $entity) - ->getSettableOptions(\Drupal::currentUser()); + $options = $item->getSettableOptions($this->currentUser()); // Add an empty option if the widget needs one. if ($empty_label = $this->getEmptyLabel()) { @@ -201,4 +205,16 @@ protected function sanitizeLabel(&$label) { */ protected function getEmptyLabel() { } + /** + * Gets the current active user. + * + * @return \Drupal\Core\Session\AccountInterface + */ + protected function currentUser() { + if (!$this->currentUser) { + $this->currentUser = \Drupal::currentUser(); + } + return $this->currentUser; + } + } diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php index d2e9fe8..a689443 100644 --- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php @@ -50,6 +50,13 @@ abstract class FilterPluginBase extends HandlerBase implements CacheablePluginInterface { /** + * Current user object. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Contains the actual value of the field,either configured in the views ui * or entered in the exposed filters. */ @@ -1381,9 +1388,8 @@ public function storeExposedInput($input, $status) { } // Check if we store exposed value for current user. - $user = \Drupal::currentUser(); $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']); - $intersect_rids = array_intersect(array_keys($allowed_rids), $user->getRoles()); + $intersect_rids = array_intersect(array_keys($allowed_rids), $this->currentUser()->getRoles()); if (empty($intersect_rids)) { return; } @@ -1463,24 +1469,15 @@ protected static function arrayFilterZero($var) { } /** - * {@inheritdoc} - */ - public function isCacheable() { - return TRUE; - } - - /** - * {@inheritdoc} + * Gets the current active user. + * + * @return \Drupal\Core\Session\AccountInterface */ - public function getCacheContexts() { - $cache_contexts = []; - // An exposed filter allows the user to change a view's filters. They accept - // input from GET parameters, which are part of the URL. Hence a view with - // an exposed filter is cacheable per URL. - if ($this->isExposed()) { - $cache_contexts[] = 'url'; - } - return $cache_contexts; + protected function currentUser() { + if (!$this->currentUser) { + $this->currentUser = \Drupal::currentUser(); + } + return $this->currentUser; } }