LanguageSelectionPageConditionIgnoreNeutral.php in Language Selection Page 8.2
File
src/Plugin/LanguageSelectionPageCondition/LanguageSelectionPageConditionIgnoreNeutral.php
View source
<?php
declare (strict_types=1);
namespace Drupal\language_selection_page\Plugin\LanguageSelectionPageCondition;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\language_selection_page\LanguageSelectionPageConditionBase;
use Drupal\language_selection_page\LanguageSelectionPageConditionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class LanguageSelectionPageConditionIgnoreNeutral extends LanguageSelectionPageConditionBase implements LanguageSelectionPageConditionInterface {
protected $routeMatch;
public function __construct(RouteMatchInterface $route_match, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[$this
->getPluginId()] = [
'#title' => $this
->t('Ignore untranslatable (language neutral) entities.'),
'#type' => 'checkbox',
'#default_value' => $this->configuration[$this
->getPluginId()],
'#description' => $this
->t('Do not redirect to the language selection page if the entity on the page being viewed is not translatable (such as when it is language neutral, or if the content type it belongs to is not translatable).'),
];
return $form;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('current_route_match'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
if ($this->configuration[$this
->getPluginId()]) {
foreach ($this->routeMatch
->getParameters() as $parameter) {
if ($parameter instanceof ContentEntityInterface) {
if (!$parameter
->isTranslatable()) {
return $this
->block();
}
}
}
}
return $this
->pass();
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$form_state
->set($this
->getPluginId(), (bool) $form_state
->get($this
->getPluginId()));
}
}