Skip to content
Snippets Groups Projects
Commit 615238bd authored by Jon Minder's avatar Jon Minder
Browse files

Support generic node patterns.

parent baea6d59
No related branches found
No related tags found
No related merge requests found
......@@ -10,11 +10,27 @@
*/
function frontend_routing_form_node_form_alter(&$form, &$form_state) {
$nodes = \Drupal::state()->get('frontend_routing.settings');
$nodes = array_combine($nodes, $nodes);
$node = $form_state->getFormObject()->getEntity();
if (!$node->isNew() && in_array($node->id(), $nodes)) {
$form['path']['#access'] = FALSE;
$form['path_replace']['#weight'] = $form['path']['#weight'];
$form['path_replace']['#markup'] = '<strong>' . t('This node is managed by Frontend Routing.') . '</strong>';
// Check if the alias matches for a single alias.
if (!empty($nodes)) {
$nodes = array_combine($nodes, $nodes);
$node = $form_state->getFormObject()->getEntity();
if (!$node->isNew() && in_array($node->id(), $nodes)) {
$form['path']['#access'] = FALSE;
$form['path_replace']['#weight'] = $form['path']['#weight'];
$form['path_replace']['#markup'] = '<strong>' . t('This node is managed by Frontend Routing.') . '</strong>';
}
}
// Check if the alias matches for the pattern.
$keys = \Drupal::config('frontend_routing.settings')->get('keys') ?? [];
foreach ($keys as $key => $value) {
if (empty($value['entity_id']) && !empty($value['bundle'])) {
if (!$node->isNew() && $node->bundle() === $value['bundle']) {
$form['path']['#access'] = FALSE;
$form['path_replace']['#weight'] = $form['path']['#weight'];
$form['path_replace']['#markup'] = '<strong>' . t('This node is managed by Frontend Routing.') . '</strong>';
}
}
}
}
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\frontend_routing\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -23,7 +24,8 @@ final class SettingsForm extends ConfigFormBase {
public function __construct(
protected ConfigFactoryInterface $config_factory,
protected StateInterface $state,
protected EntityTypeManagerInterface $entityTypeManager
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
) {
parent::__construct($config_factory);
}
......@@ -36,6 +38,7 @@ final class SettingsForm extends ConfigFormBase {
$container->get('config.factory'),
$container->get('state'),
$container->get('entity_type.manager'),
$container->get('entity_type.bundle.info'),
);
}
......@@ -93,11 +96,29 @@ final class SettingsForm extends ConfigFormBase {
'#disabled' => TRUE,
];
$form['existing']['table'][$key]['bundle'] = [
'#title' => $this->t('Node Type'),
'#type' => 'select',
'#options' => $this->getNodeTypes(),
'#required' => TRUE,
'#default_value' => !empty($keys[$key]['bundle']) ? $keys[$key]['bundle'] : NULL,
];
$required = TRUE;
// If we have a generic alias, we do not require a node.
if (!empty($aliases)) {
$alias = reset($aliases);
if (!empty($alias) && strpos($alias, '/:slug') !== FALSE) {
$required = FALSE;
}
}
$form['existing']['table'][$key]['entity_id'] = [
'#title' => $this->t('Node'),
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#required' => TRUE,
'#required' => $required,
'#disabled' => !$required,
'#default_value' => !empty($nodes[$key]) ? $this->entityTypeManager->getStorage('node')->load($nodes[$key]) : NULL,
];
......@@ -160,6 +181,9 @@ final class SettingsForm extends ConfigFormBase {
public function validateForm(array &$form, FormStateInterface $form_state): void {
if ($form_state->getValue('key')) {
if (empty($form_state->getValue('bundle'))) {
$form_state->setErrorByName('bundle', $this->t('Node Type required.'));
}
if (empty($form_state->getValue('entity_id'))) {
$form_state->setErrorByName('entity_id', $this->t('Node is required.'));
}
......@@ -204,7 +228,7 @@ final class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state): void {
$nodes = [];
// Save data from the table
// Save data from the table.
$keys = $this->config('frontend_routing.settings')->get('keys') ?? [];
// Add a new item.
......@@ -212,7 +236,7 @@ final class SettingsForm extends ConfigFormBase {
if (!empty($values['key'])) {
$key = !empty($keys[$values['key']]) ? $keys[$values['key']] : [];
$aliases = $key['aliases'] ?? [];
// Prepend alias with / if it does not have one
// Prepend alias with / if it does not have one.
if (strpos($values['alias'], '/') !== 0) {
$values['alias'] = '/' . $values['alias'];
}
......@@ -223,6 +247,8 @@ final class SettingsForm extends ConfigFormBase {
$nodes[$values['key']] = $values['entity_id'];
$key['bundle'] = $values['bundle'];
$this->config('frontend_routing.settings')
->set('keys', $keys)
->save();
......@@ -231,7 +257,10 @@ final class SettingsForm extends ConfigFormBase {
$table = $form_state->getValue('table');
if (is_array($table)) {
foreach ($table as $key => $value) {
$nodes[$key] = $value['entity_id'];
$keys[$key]['bundle'] = $value['bundle'];
if (!empty($value['entity_id'])) {
$nodes[$key] = $value['entity_id'];
}
}
$this->config('frontend_routing.settings')
->set('keys', $keys)->save();
......@@ -280,4 +309,18 @@ final class SettingsForm extends ConfigFormBase {
}
}
/**
* Get Node bundles.
*
* @return array
*/
private function getNodeTypes() {
$list = [];
$bundles = $this->entityTypeBundleInfo->getBundleInfo('node');
foreach ($bundles as $key => $bundle) {
$list[$key] = $bundle['label'];
}
return $list;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment