You are here

NeutralPathAliasStorage.php in Language neutral aliases 8.2

Same filename and directory in other branches
  1. 3.x src/NeutralPathAliasStorage.php

File

src/NeutralPathAliasStorage.php
View source
<?php

namespace Drupal\language_neutral_aliases;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\path_alias\PathAliasStorage;

/**
 * Extend PathAliasStorage to save aliases with neutral language.
 */
class NeutralPathAliasStorage extends PathAliasStorage {

  /**
   * {@inheritdoc}
   */
  public function create(array $values = []) {
    $entity = parent::create($values);
    $langKey = $entity
      ->getEntityType()
      ->getKey('langcode');
    $entity->{$langKey} = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function save(EntityInterface $entity) {
    $langKey = $entity
      ->getEntityType()
      ->getKey('langcode');
    if ($langKey && $entity->{$langKey} != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
      $entity->{$langKey} = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    }
    return parent::save($entity);
  }

}

Classes

Namesort descending Description
NeutralPathAliasStorage Extend PathAliasStorage to save aliases with neutral language.