1

I want set the default frontpage of both my local and production site site.

In my local environment I created a content it has node id (/node/20) and it has URL alias of /homepage. In the Basic site settings I set my default frontpage to /homepage

In the production environment I also create a content it has node id (/node/33) and it has URL alias of /homepage. In the Basic site settings I set my default frontpage to /homepage.

Whenever I deploy something to the production environment, I export the configuration of my local environment (in the Drupal CMS) and I will import to the production environment. But whenever I import my configuration to the production my default frontpage is setting my homepage to /node/33, why is drupal doing this? I specifically set the default page to /homepage.

I'm using drupal 8 by the way.

1
  • I know that maybe is not the best option for you, but maybe you should get a copy of the production database and replace your local database with the new one. Export every change you already have on your local build to features in order to be able to revert them and keep working on those.
    – Ismini
    Oct 27, 2017 at 15:33

2 Answers 2

1

Drupal core expects only internal URL (node/123) in the page.front setting. See \Drupal\Core\Path\PathMatcher::isFrontPage.

To be able to set page alias in the front page settings, I have created a workaround. I made a replacement of the path.matcher using an alias fallback:

mymodule/src/MymoduleServiceProvider.php:

<?php

namespace Drupal\mymodule;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Modifies the path matcher service.
 */
class MymoduleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {   
    // Override the path matcher for the alias fallback.
    $definition = $container->getDefinition('path.matcher');
    $definition->setClass('Drupal\mymodule\AliasFallbackPathMatcher')
      ->addArgument(new Reference('path.alias_manager'));
  }
}

mymodule/src/AliasFallbackPathMatcher.php:

<?php

namespace Drupal\mymodule;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Path\PathMatcher;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * The path matcher with alias fallback implementation.
 */
class AliasFallbackPathMatcher extends PathMatcher {

  /**
   * The alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * Creates a new PathMatcher.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   *   The alias manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, AliasManagerInterface $alias_manager) {
    parent::__construct($config_factory, $route_match);
    $this->aliasManager = $alias_manager;
  }

  /**
   * {@inheritdoc}
   *
   * Support path alias for the front page path.
   */
  protected function getFrontPagePath() {
    if (!isset($this->frontPage)) {
      $path = parent::getFrontPagePath();
      $this->frontPage = $this->aliasManager->getPathByAlias($path);
    }
    return $this->frontPage;
  }

}
0

Someone may correct this but I'm fairly sure that's the intended behaviour.

That setting gets stored in the database as the actual Drupal path (/node/33) rather then the alias. In other words, that field gets stored as a path and translated to an alias on display. Which is kind of the opposite to how you use aliases elsewhere.

There's probably many of reasons for that. But I suspect it may have something to do with the fact that aliases are fluid, they get regenerated and reassigned all the time whereas a path remains relatively constant. A change to an alias could in effect change your front page which is bad. There are also a bunch of low level classes/functions that need to know your front page but don't have access to the path module.

It probably depends on how you're importing your config but I suspect something may be messing with/regenerating your aliases on import.

1
  • Thanks for your input, but the node id of my homepage in my local and production are different. How can I resolve this problem? Oct 27, 2017 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.