PHP code example of mrmilu / alias_subpaths
1. Go to this page and download the library: Download mrmilu/alias_subpaths library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mrmilu / alias_subpaths example snippets
\Drupal::service('alias_subpaths.context_manager')->getContextBag();
\Drupal::service('alias_subpaths.context_manager')->getProcessedContextBag();
namespace Drupal\alias_subpaths_node\Plugin\ArgumentProcessor;
use Drupal\alias_subpaths\Plugin\ArgumentProcessorBase;
use Drupal\alias_subpaths\Plugin\Attribute\ArgumentProcessor;
/**
* Provides an ArgumentProcessor for nodes.
*/
#[ArgumentProcessor(
id: 'node', route_name: 'entity.node.canonical',
)]
class NodeArgumentProcessor extends ArgumentProcessorBase {
/**
* {@inheritDoc}
*/
protected function getId() {
return 'entity:node:' . $this->contextBag->getRouteInfo()['arguments'][0]->bundle();
}
}
$settings['alias_subpaths__argument_resolver_handler_class'] = '\Drupal\alias_subpaths\ArgumentResolverHandler\SettingsArgumentResolverHandler';
$settings['alias_subpaths__allowed_arguments_types'] = [
'route_1' => [
'argument_type_1',
'argument_type_2',
// ...
'argument_type_n',
],
'route_2' => [
// ...
],
// ...
];
$settings['alias_subpaths__argument_resolver_class'] = [
'argument_type_1' => '\Drupal\my_project\ArgumentResolver\ArgumentType1Resolver',
'argument_type_2' => '\Drupal\my_project\ArgumentResolver\ArgumentType2Resolver',
// ...
'argument_type_3' => '\Drupal\my_project\ArgumentResolver\ArgumentTypeNResolver',
];
// Specify ArgumentResolverHandler (Optional)
$settings['alias_subpaths__argument_resolver_handler_class'] = '\Drupal\alias_subpaths\ArgumentResolverHandler\SettingsArgumentResolverHandler';
// Set allowed argument types for our project.
$settings['alias_subpaths__allowed_arguments_types'] = [
'entity:node:filtered_page' => [
'page',
'article',
'tags',
],
'entity:taxonomy_term:tags' => [
'page',
],
];
// Set the resolver classes for each argument type.
$settings['alias_subpaths__argument_resolver_class'] = [
'page' => '\Drupal\my_project\ArgumentResolver\PageArgumentResolver',
'article' => '\Drupal\my_project\ArgumentResolver\ArticleArgumentResolver',
'tags' => '\Drupal\my_project\ArgumentResolver\TagsArgumentResolver',
];
namespace Drupal\my_project\ArgumentResolver;
use Drupal\alias_subpaths\ArgumentResolver\BaseArgumentResolver;
use Drupal\node\Entity\Node;
class PageArgumentResolver extends BaseArgumentResolver{
const PARAM_NAME = 'page';
public function resolve($value): bool {
$nids = \Drupal::entityQuery('node')
->accessCheck()
->condition('type', 'page')
->condition('field_url_key_arg', $value)
->range(0, 1)
->execute();
return !empty($nids);
}
public function getProcessedValue($value): mixed {
$nids = \Drupal::entityQuery('node')
->accessCheck()
->condition('type', 'page')
->condition('field_url_key_arg', $value)
->range(0, 1)
->execute();
return Node::load(reset($nids));
}
public function getDefaultValue(): mixed {
return Node::load(1);
}
}
namespace Drupal\my_project\ArgumentResolver;
use Drupal\alias_subpaths\ArgumentResolver\BaseArgumentResolver;
use Drupal\node\Entity\Node;
class ArticleArgumentResolver extends BaseArgumentResolver {
const PARAM_NAME = 'article';
public function resolve($value): bool {
$nids = \Drupal::entityQuery('node')
->accessCheck()
->condition('type', 'article')
->condition('title', $value)
->range(0, 1)
->execute();
return !empty($nids);
}
public function getProcessedValue($value): mixed {
$nids = \Drupal::entityQuery('node')
->accessCheck()
->condition('type', 'article')
->condition('title', $value)
->range(0, 1)
->execute();
return Node::load(reset($nids));
}
public function getDefaultValue(): mixed {
return Node::load(1);
}
}
namespace Drupal\my_project\ArgumentResolver;
use Drupal\alias_subpaths\ArgumentResolver\BaseArgumentResolver;
use Drupal\taxonomy\Entity\Term;
class TagsArgumentResolver extends BaseArgumentResolver {
const PARAM_NAME = 'tags';
private array|int $tids;
public function resolve($value): bool {
$tids = \Drupal::entityQuery('taxonomy_term')
->accessCheck()
->condition('vid', 'tags')
->condition('name', $value)
->range(0, 1)
->execute();
$this->tids = $tids;
return !empty($tids);
}
public function getProcessedValue($value): mixed {
return Term::load(reset($this->tids));
}
public function getDefaultValue(): mixed {
return NULL;
}
}
/**
* Implements hook_preprocess_node().
*/
function alias_subpaths_examples_preprocess_node(array &$variables) {
$node = $variables['node'];
if ($node->bundle() === 'filtered_page') {
/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = \Drupal::service('request_stack')->getCurrentRequest();
/** @var \Drupal\alias_subpaths\ContextManager $contextManager */
$contextManager = \Drupal::service('alias_subpaths.context_manager');
$variables['alias_subpaths_arguments'] = $contextManager->getContextBag($request->getPathInfo())->getProcessedContent();
}
}