Skip to content
Snippets Groups Projects
Commit 58ebc73e authored by Andrey Tymchuk's avatar Andrey Tymchuk Committed by A.Tymchuk
Browse files

Issue #3386017 by GuillaumeG, dalin, bdimaggio, WalkingDexter: Act on all...

Issue #3386017 by GuillaumeG, dalin, bdimaggio, WalkingDexter: Act on all entites, not just translatable entities
parent 6d546823
No related branches found
No related tags found
No related merge requests found
......@@ -5,3 +5,4 @@ id: default
label: Default
code: null
path: ''
mode: translatable
......@@ -16,3 +16,6 @@ content_translation_redirect.entity.*:
path:
type: path
label: 'Redirect path'
mode:
type: string
label: 'Translation mode'
......@@ -15,6 +15,21 @@ interface ContentTranslationRedirectInterface extends ConfigEntityInterface {
*/
public const DEFAULT_ID = 'default';
/**
* Act on translatable entities.
*/
public const MODE_TRANSLATABLE = 'translatable';
/**
* Act on untranslatable entities.
*/
public const MODE_UNTRANSLATABLE = 'untranslatable';
/**
* Act on all entities.
*/
public const MODE_ALL = 'all';
/**
* Sets the redirect status code.
*
......@@ -46,10 +61,10 @@ interface ContentTranslationRedirectInterface extends ConfigEntityInterface {
/**
* Gets the redirect path.
*
* @return string|null
* @return string
* The redirect path.
*/
public function getPath(): ?string;
public function getPath(): string;
/**
* Gets the redirect Url object.
......@@ -59,6 +74,40 @@ interface ContentTranslationRedirectInterface extends ConfigEntityInterface {
*/
public function getUrl(): ?Url;
/**
* Sets the translation mode.
*
* @param string $mode
* The translation mode.
*
* @return $this
*/
public function setTranslationMode(string $mode): ContentTranslationRedirectInterface;
/**
* Gets the translation mode.
*
* @return string
* The translation mode.
*/
public function getTranslationMode(): string;
/**
* Should redirect only happen on translatable entities?
*
* @return bool
* Whether we should act on translatable entity only or not.
*/
public function translatableEntityOnly(): bool;
/**
* Should redirect only happen on untranslatable entities?
*
* @return bool
* Whether we should act on untranslatable entity only or not.
*/
public function untranslatableEntityOnly(): bool;
/**
* Returns whether this redirect is locked.
*
......
......@@ -40,6 +40,7 @@ class ContentTranslationRedirectListBuilder extends ConfigEntityListBuilder {
$header['label'] = $this->t('Type');
$header['code'] = $this->t('Redirect status');
$header['path'] = $this->t('Redirect path');
$header['mode'] = $this->t('Act on');
return $header + parent::buildHeader();
}
......@@ -54,6 +55,7 @@ class ContentTranslationRedirectListBuilder extends ConfigEntityListBuilder {
$row['label'] = $entity->label();
$row['code'] = $code ? ContentTranslationRedirect::getStatusCodes()[$code] : $this->t('Not specified');
$row['path'] = $path ? ($path === '/' ? $this->t('Front page') : Link::fromTextAndUrl($path, $entity->getUrl())) : $this->t('Original content');
$row['mode'] = ContentTranslationRedirect::getTranslationModes()[$entity->getTranslationMode()];
return $row + parent::buildRow($entity);
}
......
......@@ -40,6 +40,7 @@ use Drupal\Core\Url;
* "label",
* "code",
* "path",
* "mode",
* }
* )
*/
......@@ -71,7 +72,14 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
*
* @var string
*/
protected $path;
protected $path = '';
/**
* The translation mode.
*
* @var string
*/
protected $mode = self::MODE_TRANSLATABLE;
/**
* {@inheritdoc}
......@@ -99,7 +107,7 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
/**
* {@inheritdoc}
*/
public function getPath(): ?string {
public function getPath(): string {
return $this->path;
}
......@@ -110,6 +118,35 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
return $this->path ? Url::fromUserInput($this->path) : NULL;
}
/**
* {@inheritdoc}
*/
public function setTranslationMode(string $mode): ContentTranslationRedirectInterface {
$this->mode = $mode;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTranslationMode(): string {
return $this->mode;
}
/**
* {@inheritdoc}
*/
public function translatableEntityOnly(): bool {
return $this->mode === ContentTranslationRedirectInterface::MODE_TRANSLATABLE;
}
/**
* {@inheritdoc}
*/
public function untranslatableEntityOnly(): bool {
return $this->mode === ContentTranslationRedirectInterface::MODE_UNTRANSLATABLE;
}
/**
* {@inheritdoc}
*/
......@@ -180,4 +217,18 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
];
}
/**
* Returns translation modes.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
* Translation modes.
*/
public static function getTranslationModes(): array {
return [
ContentTranslationRedirectInterface::MODE_TRANSLATABLE => t('Translatable entities'),
ContentTranslationRedirectInterface::MODE_UNTRANSLATABLE => t('Untranslatable entities'),
ContentTranslationRedirectInterface::MODE_ALL => t('All entities'),
];
}
}
......@@ -62,10 +62,13 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
* The event to process.
*/
public function onRequestCheckRedirect(RequestEvent $event): void {
$entity = $this->getEntity();
// Check that the site has more than one language.
if (!$this->languageManager->isMultilingual()) {
return;
}
// Check the translatable entity.
if (!$entity || !$entity->isTranslatable()) {
$entity = $this->getEntity();
if ($entity === NULL) {
return;
}
......@@ -81,6 +84,16 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
return;
}
// Check whether we should act on translatable entity only or not.
if ($redirect->translatableEntityOnly() && !$entity->isTranslatable()) {
return;
}
// Check whether we should act on untranslatable entity only or not.
if ($redirect->untranslatableEntityOnly() && $entity->isTranslatable()) {
return;
}
$url = Url::fromRoute('<current>');
$current_url = $url->setAbsolute()->toString();
......
......@@ -77,6 +77,13 @@ class ContentTranslationRedirectForm extends EntityForm {
'#description' => $this->t('Path to redirect. Leave blank to redirect to original content.'),
'#default_value' => $redirect->getPath(),
];
$form['mode'] = [
'#type' => 'radios',
'#title' => $this->t('Act on'),
'#options' => ContentTranslationRedirect::getTranslationModes(),
'#default_value' => $redirect->getTranslationMode(),
'#required' => TRUE,
];
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment