url direct access #2936
This commit is contained in:
5
web/modules/custom/decoupled/decoupled.info.yml
Normal file
5
web/modules/custom/decoupled/decoupled.info.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
name: 'decoupled'
|
||||
type: module
|
||||
description: 'helpers for progressive decoupling'
|
||||
core_version_requirement: ^8.8 || ^9.2 || ^10.2
|
||||
package: 'Caravane'
|
105
web/modules/custom/decoupled/decoupled.module
Normal file
105
web/modules/custom/decoupled/decoupled.module
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains decoupled.module.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
// use Drupal\pathauto\PathautoPatternInterface;
|
||||
// use Drupal\pathauto\PathautoPatternManagerInterface;
|
||||
use Drupal\pathauto\Entity\PathautoPattern;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function decoupled_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
// Main module help for the materio_decoupled module.
|
||||
case 'help.page.materio_decoupled':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('helpers for progressive decoupling') . '</p>';
|
||||
return $output;
|
||||
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_page_attachments().
|
||||
* @param array $attachments
|
||||
*/
|
||||
function decoupled_page_attachments(array &$attachments) {
|
||||
$redirect = false;
|
||||
$current_path = \Drupal::service('path.current')->getPath();
|
||||
$route_name = \Drupal::routeMatch()->getRouteName();
|
||||
// $route_parameters = \Drupal::routeMatch()->getParameters()->all();
|
||||
// ksm($route_parameters);
|
||||
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
$is_front = \Drupal::service('path.matcher')->isFrontPage();
|
||||
if (!$is_front) {
|
||||
$redirect = true;
|
||||
}
|
||||
$entity_type = null;
|
||||
$entity_bundle = null;
|
||||
$entity_id = null;
|
||||
$entity_uuid = null;
|
||||
$base_alias = null;
|
||||
|
||||
foreach (['node', 'taxonomy_term'] as $type) {
|
||||
$entity = \Drupal::routeMatch()->getParameter($type);
|
||||
if($entity){
|
||||
$entity_type = $type;
|
||||
$entity_bundle = $entity->bundle();
|
||||
$entity_id = $entity->id();
|
||||
$entity_uuid = $entity->uuid();
|
||||
|
||||
// $pathauto_patterns = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->loadByProperties(['type' => "canonical_entities:".$entity_type]);
|
||||
// foreach ($pathauto_patterns as $id => $pattern) {
|
||||
// $conditions = $pattern->getSelectionConditions();
|
||||
// $condition_config = $conditions->getConfiguration();
|
||||
// foreach ($condition_config as $key => $config) {
|
||||
// $bundles = array_keys($config['bundles']);
|
||||
// if (in_array($entity_bundle, $bundles)) {
|
||||
// $motif = $pattern->getPattern();
|
||||
// preg_match('/(\/.+)+\/\[[^]]+\]/', $motif, $matches);
|
||||
// $base_alias = $matches[1];
|
||||
// break 2;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// route_parameters:'".json_encode($route_parameters)."',\n
|
||||
$js_str = "var drupalDecoupled = {\n
|
||||
sys_path:'".$current_path."',\n
|
||||
route_name:'".$route_name."',\n
|
||||
is_front:".($is_front ? 'true':'false').",\n
|
||||
base_alias:'".$base_alias."',\n
|
||||
redirect:".($redirect ? 'true':'false').",\n
|
||||
lang_code:'".$current_language."',\n
|
||||
entity_type:'".$entity_type."',\n
|
||||
entity_bundle:'".$entity_bundle."',\n
|
||||
entity_id:'".$entity_id."',\n
|
||||
entity_uuid:'".$entity_uuid."',\n
|
||||
};";
|
||||
|
||||
$attachments['#attached']['html_head'][] = [
|
||||
[
|
||||
'#type' => 'html_tag',
|
||||
'#tag' => 'script',
|
||||
'#value' => $js_str,
|
||||
'#weight' => -999,
|
||||
'#group' => 'decoupled'
|
||||
],
|
||||
// A key, to make it possible to recognize this HTML element when altering.
|
||||
'decoupled',
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user