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

Issue #3363449 by sakthi_dev, samaphp, WalkingDexter: Option to redirect to...

Issue #3363449 by sakthi_dev, samaphp, WalkingDexter: Option to redirect to home page if targeted translation is not exists
parent 47884769
No related branches found
Tags 8.x-1.0-alpha2
No related merge requests found
......@@ -4,3 +4,4 @@ dependencies: { }
id: default
label: Default
code: null
path: ''
......@@ -13,3 +13,6 @@ content_translation_redirect.entity.*:
code:
type: integer
label: 'Redirect status'
path:
type: path
label: 'Redirect path'
......@@ -4,6 +4,7 @@ namespace Drupal\content_translation_redirect;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* Provides an interface defining a Content Translation Redirect entity.
......@@ -39,6 +40,30 @@ interface ContentTranslationRedirectInterface extends ConfigEntityInterface {
*/
public function getStatusCodeLabel(): TranslatableMarkup;
/**
* Sets the redirect path.
*
* @param string $path
* The redirect path.
*/
public function setPath(string $path): void;
/**
* Gets the redirect path.
*
* @return string|null
* The redirect path.
*/
public function getPath(): ?string;
/**
* Gets the redirect Url object.
*
* @return \Drupal\Core\Url|null
* The redirect Url object.
*/
public function getUrl(): ?Url;
/**
* Returns whether this redirect is locked.
*
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* Defines the Content Translation Redirect entity.
......@@ -39,6 +40,7 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
* "id",
* "label",
* "code",
* "path",
* }
* )
*/
......@@ -65,6 +67,13 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
*/
protected $code;
/**
* The redirect path.
*
* @var string
*/
protected $path;
/**
* {@inheritdoc}
*/
......@@ -86,6 +95,27 @@ class ContentTranslationRedirect extends ConfigEntityBase implements ContentTran
return $this->code ? static::getStatusCodes()[$this->code] : t('Not specified');
}
/**
* {@inheritdoc}
*/
public function setPath(string $path): void {
$this->path = $path;
}
/**
* {@inheritdoc}
*/
public function getPath(): ?string {
return $this->path;
}
/**
* {@inheritdoc}
*/
public function getUrl(): ?Url {
return $this->path ? Url::fromUserInput($this->path) : NULL;
}
/**
* {@inheritdoc}
*/
......
......@@ -75,23 +75,23 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
return;
}
$url = Url::fromRoute('<current>')->setAbsolute();
$current_url = $url->toString();
// Check the redirect entity with a status code.
$redirect = $this->storage->loadByEntity($entity);
if (!$redirect || !$redirect->getStatusCode()) {
return;
}
$url = Url::fromRoute('<current>');
$current_url = $url->setAbsolute()->toString();
$url->setOption('language', $entity->getUntranslated()->language());
$redirect_url = $url->toString();
$url = $redirect->getUrl() ?? $url->setOption('language', $entity->getUntranslated()->language());
$redirect_url = $url->setAbsolute()->toString();
// Check the difference between URLs.
if ($current_url === $redirect_url) {
return;
}
// Check the redirect entity with a status code.
$redirect = $this->storage->loadByEntity($entity);
if (!$redirect || !$redirect->getStatusCode()) {
return;
}
// Set the redirect response.
$response = new LocalRedirectResponse($redirect_url, $redirect->getStatusCode());
$response->addCacheableDependency($url);
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\PathElement;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -69,9 +70,25 @@ class ContentTranslationRedirectForm extends EntityForm {
'#default_value' => $redirect->getStatusCode(),
'#empty_option' => $this->t('- Not specified -'),
];
$form['path'] = [
'#type' => 'path',
'#title' => $this->t('Redirect path'),
'#convert_path' => PathElement::CONVERT_NONE,
'#description' => $this->t('Path to redirect. Leave blank to redirect to original content.'),
'#default_value' => $redirect->getPath(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (($path = $form_state->getValue('path')) && $path[0] !== '/') {
$form_state->setErrorByName('path', $this->t("The path '%path' has to start with a slash.", ['%path' => $path]));
}
}
/**
* {@inheritdoc}
*/
......
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