Commit 97021f5c authored by ekes's avatar ekes
Browse files

Issue #3293377: Support domain path

parent dc0556f0
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -17,14 +17,20 @@
  ],
  "support": {
    "issues": "https://drupal.org/project/issues/domain_group",
    "source": "https://cgit.drupalcode.org/domain_group"
    "source": "https://git.drupalcode.org/project/domain_group"
  },
  "license": "GPL-2.0+",
  "minimum-stability": "dev",
  "require": {
    "drupal/core": "^8.8 || ^9",
    "drupal/core": "^^9",
    "drupal/group": "~1.0",
    "drupal/domain": "~1.0",
    "drupal/domain_site_settings": "~1.0"
  },
  "require-dev": {
    "drupal/domain_path": "^1.x-dev"
  },
  "suggest": {
    "drupal/domain_path": "Path aliases per domain."
  }
}
+31 −24
Original line number Diff line number Diff line
@@ -188,43 +188,50 @@ function domain_group_module_implements_alter(&$implementations, $hook) {
 * Implements hook_form_alter().
 */
function domain_group_form_alter(&$form, &$form_state, $form_id) {

  // Support for domain_path.
  $domain_path = NULL;
  if (isset($form['path']['widget'])
    && isset($form['path']['widget'][0])
    && isset($form['path']['widget'][0]['domain_path'])
  ) {
    $domain_path =& $form['path']['widget'][0]['domain_path'];
  }
  elseif (isset($form['domain_path'])) {
    $domain_path =& $form['domain_path'];
  }

  if ($domain_path
    && ($object = $form_state->getFormObject())
    && !empty($object) && is_callable([$object, 'getEntity'])
    && ($entity = $object->getEntity())
  ) {
    $domain_ids = Element::children($form['path']['widget'][0]['domain_path']);
    $domain_ids = array_flip($domain_ids); 
    unset($domain_ids['domain_path_delete']);
    $domain_ids = Element::children($domain_path);
    $domain_ids_remove = array_flip($domain_ids); 
    unset($domain_ids_remove['domain_path_delete']);
    if (($active = \Drupal::service('domain.negotiator')->getActiveDomain())
      && assert($active instanceof DomainInterface)
    ) {
      if (!$active->isDefault()) {
        // On a group domain, only show that.
        unset($domain_ids[$active->id()]);
      }
      elseif ($domains = \Drupal::service('domain_group_resolver')->getEntityGroupDomains($entity)) {
      // Always show the active domain, be it default, or microsite.
      unset($domain_ids_remove[$active->id()]);
      // On the default domain might show more, or all, domains.
      if ($active->isDefault()) {
        if ($domains = \Drupal::service('domain_group_resolver')->getEntityGroupDomains($entity)) {
          // On default domain with an entity has groups with domain.
          // Show all of these domain groups, and default domain.
        $domains[$active->id()] = $active;
        $domain_ids = array_diff_key($domain_ids, $domains);
          $domain_ids_remove = array_diff_key($domain_ids_remove, $domains);
        }
        elseif ($domain = \Drupal::service('domain_group_resolver')->getCurrentRouteGroupDomain()) {
          // On default domain, and a group route, probably a new entity form.
        // Show domain group and default. 
        unset($domain_ids[$active->id()]);
        unset($domain_ids[$domain->id()]);
          unset($domain_ids_remove[$domain->id()]);
        }
        else {
        // Just the default domain.
        unset($domain_ids[$active->id()]);
          $domain_ids_remove = [];
        }
      }

      foreach ($domain_ids as $domain_id => $index) {
        $form['path']['widget'][0]['domain_path'][$domain_id]['#access'] = FALSE;
      foreach ($domain_ids_remove as $domain_id => $index) {
        $domain_path[$domain_id]['#access'] = FALSE;
      }
    }
  }
+210 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\domain_group\Functional;

use Drupal\domain\DomainInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\domain_group\Traits\GroupCreationTrait;
use Drupal\Tests\domain_group\Traits\InitializeGroupsTrait;
use Drupal\Tests\domain_path\Functional\DomainPathTestBase;

/**
 * Tests integration with domain_path.
 *
 * @group domain_group
 */
class DomainPathIntegrationTest extends BrowserTestBase {

  use GroupCreationTrait;
  use InitializeGroupsTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  protected $strictConfigSchema = FALSE;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'domain_path',
    'field',
    'node',
    'gnode',
    'user',
    'path',
    'system',
    'group',
    'domain_site_settings',
    'domain_group',
  ];


  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $admin = $this->drupalCreateUser([
      'administer url aliases',
      'administer domain paths',
      'administer nodes',
      'bypass node access',
      'edit domain path entity',
      'add domain paths',
      'edit domain paths',
      'delete domain paths',
      'administer group',
      'bypass group access',
      'domain group settings',
    ]);
    $this->drupalLogin($admin);

    $this->initializeTestGroups();
    $this->initializeTestGroupsDomains();
    $this->initializeTestGroupContent();

    $this->config('domain_path.settings')
      ->set('entity_types', ['node' => TRUE])->save();
  }

  /**
   * Tests getting each of the domain paths.
   */
  public function testNodeForm() {
    $all_domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
    foreach ($all_domains as $domain) {
      if ($domain->isDefault()) {
        $default_domain = $domain;
        break;
      }
    }
    $groupA1_domain = $all_domains['group_' . $this->groupA1->id()];
    assert($groupA1_domain instanceof DomainInterface);

    // Node form outside group context.
    // All domain path fields available.
    $domain_paths_check = [];
    $this->drupalGet('node/add/article');
    $page = $this->getSession()->getPage();
    $article_title = $this->randomString();
    $page->fillField('title[0][value]', $article_title);
    foreach ($all_domains as $domain) {
      $domain_alias_value = '/' . $this->randomMachineName(8);
      $page->fillField('domain_path[' . $domain->id() . '][path]', $domain_alias_value);
      $domain_paths_check[$domain->id()] = $domain_alias_value;
    }
    $page->pressButton('edit-submit');
    // Check they got saved.
    $node = $this->drupalGetNodeByTitle($article_title);
    $this->drupalGet('node/' . $node->id() . '/edit');
    $session = $this->assertSession();
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals('domain_path[' . $domain_id . '][path]', $domain_alias_value);
    }

    // Default domain, create group content.
    // Just default and group domain path fields.
    $domain_paths_check = [];
    $this->drupalGet('group/'. $this->groupA1->id() . '/content/create/group_node%3Aarticle');
    $article_title = $this->randomString();
    $page->fillField('title[0][value]', $article_title);
    $active_domains = [
      $default_domain->id() => $default_domain,
      'group_' . $this->groupA1->id() => $groupA1_domain,
    ];
    $inactive_domains = $all_domains;
    unset($inactive_domains[$default_domain->id()]);
    unset($inactive_domains['group_' . $this->groupA1->id()]);
    foreach ($active_domains as $domain) {
      $domain_alias_value = '/' . $this->randomMachineName(8);
      $page->fillField($this->domainPathField($domain->id()), $domain_alias_value);
      $domain_paths_check[$domain->id()] = $domain_alias_value;
    }
    foreach ($inactive_domains as $domain) {
      $session->fieldNotExists($this->domainPathField($domain->id()));
    }
    $page->pressButton('edit-submit');
    // Check they got saved.
    $node = $this->drupalGetNodeByTitle($article_title);
    $this->drupalGet('node/' . $node->id() . '/edit');
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals($this->domainPathField($domain_id), $domain_alias_value);
    }
    $page->pressButton('edit-submit');
    // Content own own domain.
    // Should only show own domain.
    $this->drupalGet($groupA1_domain->getPath() . 'node/' . $node->id() . '/edit');
    unset($active_domains[$default_domain->id()]);
    $inactive_domains[$default_domain->id()] = $default_domain;
    foreach ($inactive_domains as $domain) {
      $session->fieldNotExists($this->domainPathField($domain->id()));
    }
    $session->fieldValueEquals($this->domainPathField($groupA1_domain->id()), $domain_paths_check['group_' . $this->groupA1->id()]);
    // Change value.
    $domain_paths_check[$groupA1_domain->id()] = '/' . $this->randomMachineName();
    $page->fillField($this->domainPathField($groupA1_domain->id()), $domain_paths_check[$groupA1_domain->id()]);
    $page->pressButton('edit-submit');
    // Check it on the domain.
    $this->drupalGet($groupA1_domain->getPath() . 'node/' . $node->id() . '/edit');
    $session->fieldValueEquals($this->domainPathField($groupA1_domain->id()), $domain_paths_check['group_' . $this->groupA1->id()]);
    // Check values, including those not displayed, were maintained on the
    // default domain.
    $this->drupalGet('node/' . $node->id() . '/edit');
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals($this->domainPathField($domain_id), $domain_alias_value);
    }

    // New content on a group domain.
    $groupA3_domain = $all_domains['group_' . $this->groupA3->id()];
    assert($groupA3_domain instanceof DomainInterface);
    $domain_paths_check = [];
    $this->drupalGet($groupA3_domain->getPath() . 'group/'. $this->groupA3->id() . '/content/create/group_node%3Aarticle');
    $article_title = $this->randomString();
    $page->fillField('title[0][value]', $article_title);
    $active_domains = [
      'group_' . $this->groupA3->id() => $groupA3_domain,
    ];
    $inactive_domains = $all_domains;
    unset($inactive_domains['group_' . $this->groupA3->id()]);
    foreach ($active_domains as $domain) {
      $domain_alias_value = '/' . $this->randomMachineName(8);
      $page->fillField($this->domainPathField($domain->id()), $domain_alias_value);
      $domain_paths_check[$domain->id()] = $domain_alias_value;
    }
    foreach ($inactive_domains as $domain) {
      $session->fieldNotExists($this->domainPathField($domain->id()));
    }
    $page->pressButton('edit-submit');
    // Check they got saved.
    $node = $this->drupalGetNodeByTitle($article_title);
    $this->drupalGet($groupA3_domain->getPath() . 'node/' . $node->id() . '/edit');
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals($this->domainPathField($domain_id), $domain_alias_value);
    }
    $page->pressButton('edit-submit');
    // And for completeness, that content on default domain.
    $this->drupalGet('node/' . $node->id() . '/edit');
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals($this->domainPathField($domain_id), $domain_alias_value);
    }
    unset($inactive_domains[$default_domain->id()]);
    foreach ($inactive_domains as $domain) {
      $session->fieldNotExists($this->domainPathField($domain->id()));
    }
    foreach ($domain_paths_check as $domain_id => $domain_alias_value) {
      $session->fieldValueEquals($this->domainPathField($domain_id), $domain_alias_value);
    }
    $session->fieldValueEquals($this->domainPathField($default_domain->id()), '');
  }

  private function domainPathField($domain_id, $property = 'path') {
    return 'domain_path[' . $domain_id . '][' . $property . ']';
  }

}