ContentTranslationRedirectForm.php in Content Translation Redirect 8
File
src/Form/ContentTranslationRedirectForm.php
View source
<?php
namespace Drupal\content_translation_redirect\Form;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityForm;
class ContentTranslationRedirectForm extends EntityForm {
use ContentTranslationRedirectFormTrait;
protected $entityTypeBundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$entity = $this->entity;
if ($entity
->isNew()) {
$form['id'] = [
'#type' => 'select',
'#title' => $this
->t('Type'),
'#description' => $this
->t('Select the entity type for which you want to add a redirect.'),
'#options' => $this
->getAvailableBundles(),
'#required' => TRUE,
];
}
$settings = [
'code' => $entity
->getStatusCode(),
'message' => $entity
->getMessage(),
];
$form += $this
->redirectSettingsForm($settings);
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
if ($entity
->isNew()) {
$entity_id = $form_state
->getValue('id');
list($entity_type_id, $bundle_id) = explode('__', $entity_id);
$entity_label = (string) $this->entityTypeManager
->getDefinition($entity_type_id)
->getLabel();
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$entity_label .= ': ' . $bundle_info[$bundle_id]['label'];
$entity
->set('label', $entity_label);
}
$entity
->setStatusCode($form_state
->getValue('code'));
$entity
->setMessage($form_state
->getValue('message'));
$status = $entity
->save();
if ($status == SAVED_NEW) {
$this
->messenger()
->addMessage($this
->t('Created the %label Content Translation Redirect.', [
'%label' => $entity
->label(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('Saved the %label Content Translation Redirect.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.content_translation_redirect.collection');
}
protected function getAvailableBundles() {
$options = [];
$entity_types = $this->entityTypeManager
->getDefinitions();
$bundles = $this->entityTypeBundleInfo
->getAllBundleInfo();
$labels = [];
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface) {
continue;
}
if (in_array($entity_type_id, $this
->getUnsupportedEntityTypes())) {
continue;
}
if (!$entity_type
->isTranslatable() || !$entity_type
->hasLinkTemplate('canonical') || !isset($bundles[$entity_type_id])) {
continue;
}
$labels[$entity_type_id] = (string) $entity_type
->getLabel() ?: $entity_type_id;
}
$storage = $this->entityTypeManager
->getStorage('content_translation_redirect');
foreach ($labels as $entity_type_id => $label) {
foreach ($bundles[$entity_type_id] as $bundle_id => $bundle_info) {
$entity_id = $entity_type_id . '__' . $bundle_id;
if (!$storage
->load($entity_id)) {
$options[$label][$entity_id] = $bundle_info['label'];
}
}
}
return $options;
}
protected function getUnsupportedEntityTypes() {
return [
'block_content',
'comment',
'menu_link_content',
'shortcut',
];
}
}