Support domain in selection criteria of page manager variant

Created on 13 July 2021, almost 4 years ago
Updated 7 January 2025, 3 months ago

I am using page manager with domain module I want to create different variant per domain but when I add variant and go to Selection criteria it does not provide Current domain option as we have in Drupal 7.
anyone help me how can I achieve this?

Feature request
Status

Active

Component

User interface

Created by

🇮🇳India sheetal-wish Indore

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇪🇸Spain guardiola86

    I'm having the same issue. I'm upgrading a site to Drupal 10, and some of my pages stopped working because they had domain in selection criteria, but now it doesn't appear on D10.

  • 🇪🇸Spain guardiola86

    In case it's useful to anyone, I'll add the code for the condition I had to create, which replicates the old one.

  • 🇪🇸Spain guardiola86
    <?php
    
    namespace Drupal\custom_module\Plugin\Condition;
    
    use Drupal\Core\Condition\ConditionPluginBase;
    use Drupal\Core\Form\FormStateInterface;
    
    /**
     * Provides a 'Current Domain' condition.
     *
     * @Condition(
     *   id = "current_domain",
     *   label = @Translation("Current Domain"),
     *   context = {
     *     "url" = @ContextDefinition("any", required = FALSE, label = @Translation("URL"))
     *   }
     * )
     */
    class CurrentDomainCondition extends ConditionPluginBase {
    
      /**
       * {@inheritdoc}
       */
      public function evaluate() {
        if ($this->isNegated()) {
          return !$this->matchDomain();
        }
        return $this->matchDomain();
      }
    
      /**
       * Checks if the current domain matches one of the configured domains.
       *
       * @return bool
       *   TRUE if the domains match, FALSE otherwise.
       */
      protected function matchDomain() {
        $configured_domains = $this->configuration['domains'] ?? [];
        $domain = \Drupal::service('domain.negotiator')->getActiveId();
        return in_array($domain, $configured_domains);
      }
    
      /**
       * {@inheritdoc}
       */
      public function defaultConfiguration() {
        return [
            'domains' => [],
          ] + parent::defaultConfiguration();
      }
    
      /**
       * {@inheritdoc}
       */
      public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $all_domains = $this->getAvailableDomains();
    
        $form['domains'] = [
          '#type' => 'checkboxes',
          '#title' => $this->t('Domains'),
          '#options' => $all_domains,
          '#default_value' => $this->configuration['domains'],
          '#description' => $this->t('Select the domains where this condition should apply.'),
          '#required' => TRUE,
        ];
    
        return parent::buildConfigurationForm($form, $form_state);
      }
    
      /**
       * {@inheritdoc}
       */
      public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        $selected_domains = array_filter($form_state->getValue('domains'));
        $this->configuration['domains'] = $selected_domains;
        parent::submitConfigurationForm($form, $form_state);
      }
    
      /**
       * {@inheritdoc}
       */
      public function summary() {
        $domains = $this->configuration['domains'] ?? [];
        if (!empty($domains)) {
          return $this->t('The current domain is one of: @domains.', ['@domains' => implode(', ', $domains)]);
        }
        return $this->t('No domains selected.');
      }
    
      /**
       * Fetch available domains for selection.
       *
       * @return array
       *   An array of available domains as key-value pairs (domain => label).
       */
      protected function getAvailableDomains() {
        $domains = [];
        if (\Drupal::moduleHandler()->moduleExists('domain')) {
          $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
          $all_domains = $domain_storage->loadMultiple();
          foreach ($all_domains as $domain) {
            // Use the machine name (ID) as the key and the label as the value.
            $machine_name = $domain->id();
            $label = $domain->label();
            $domains[$machine_name] = $label;
          }
        }
        return $domains;
      }
    
    }
    
    
Production build 0.71.5 2024