domainStorage = $domain_storage; $this->domainNegotiator = $domain_negotiator; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager')->getStorage('domain'), $container->get('domain.negotiator') ); } /** * {@inheritdoc} */ public function access(AccountInterface $account) { $id = $this->domainNegotiator->getActiveId(); $options = array_filter($this->options['domain']); return isset($options[$id]); } /** * {@inheritdoc} */ public function alterRouteDefinition(Route $route) { if ($this->options['domain']) { $route->setRequirement('_domain', (string) implode('+', $this->options['domain'])); } } /** * {@inheritdoc} */ public function summaryTitle() { $count = count($this->options['domain']); if ($count < 1) { return $this->t('No domain(s) selected'); } elseif ($count > 1) { return $this->t('Multiple domains'); } else { $domains = $this->domainStorage->loadOptionsList(); $domain = reset($this->options['domain']); return $domains[$domain]; } } /** * {@inheritdoc} */ protected function defineOptions() { $options = parent::defineOptions(); $options['domain'] = ['default' => []]; return $options; } /** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); $form['domain'] = [ '#type' => 'checkboxes', '#title' => $this->t('Domain'), '#default_value' => $this->options['domain'], '#options' => $this->domainStorage->loadOptionsList(), '#description' => $this->t('Only the checked domain(s) will be able to access this display.'), ]; } /** * {@inheritdoc} */ public function validateOptionsForm(&$form, FormStateInterface $form_state) { $domain = $form_state->getValue(['access_options', 'domain']); $domain = array_filter($domain); if (!$domain) { $form_state->setError($form['domain'], $this->t('You must select at least one domain if type is "by domain"')); } $form_state->setValue(['access_options', 'domain'], $domain); } /** * {@inheritdoc} */ public function calculateDependencies() { $dependencies = parent::calculateDependencies(); foreach (array_keys($this->options['domain']) as $id) { if ($domain = $this->domainStorage->load($id)) { $dependencies[$domain->getConfigDependencyKey()][] = $domain->getConfigDependencyName(); } } return $dependencies; } /** * {@inheritdoc} */ public function getCacheMaxAge() { return Cache::PERMANENT; } /** * {@inheritdoc} */ public function getCacheContexts() { return ['url.site']; } /** * {@inheritdoc} */ public function getCacheTags() { return []; } }