# @Email: bachir@figureslibres.io # @Filename: edlp_ajax.module # @License: GPL-V3 use Drupal\Core\Url; /** * Implements hook_page_attachments(). * @param array $attachments */ function edlp_ajax_page_attachments(array &$attachments) { $url = Url::fromRoute('edlp_ajax.entityjson'); $attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath(); // $url = Url::fromRoute('edlp_ajax.urljson'); // $attachments['#attached']['drupalSettings']['edlp_ajax']['urljson_path'] = $url->getInternalPath(); $redirect = false; $current_path = \Drupal::service('path.current')->getPath(); $is_front = \Drupal::service('path.matcher')->isFrontPage(); // do not redirect if note node, term, or custom routing if(preg_match('/^\/?node\/\d+/', $current_path) || preg_match('/^\/?taxonomy\/term\/\d+/', $current_path) || preg_match('/^\/?productions/', $current_path) || preg_match('/^\/?agenda/', $current_path) || preg_match('/^\/?studio/', $current_path)){ // TODO: search url $redirect = true; } $js_str = "var edlp = {\n sys_path:'".$current_path."',\n is_front:".($is_front ? 'true':'false').",\n redirect:".($redirect ? 'true':'false').",\n };"; $attachments['#attached']['html_head'][] = [ [ '#type' => 'html_tag', '#tag' => 'script', '#value' => $js_str, '#weight' => -999, '#group' => 'edlp' ], // A key, to make it possible to recognize this HTML element when altering. 'edlp', ]; } /** * Implements hook_theme(). */ function edlp_ajax_theme($existing, $type, $theme, $path) { // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module return array( 'edlp_ajax' => array( 'file' => 'includes/edlp_ajax.inc', 'variables' => array( 'entity_type' => 'node', 'entity' => NULL, 'view_mode' => 'default' ), ), ); } /** * Implements hook_theme_suggestions_HOOK(). */ function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) { // dpm($vars); $suggestions = []; // $node = $variables['elements']['#node']; $sanitized_view_mode = strtr($vars['view_mode'], '.', '_'); // $suggestions[] = 'edlp_ajax__' . $vars['entity_type']; $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode; // $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode; $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id(); $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode; return $suggestions; }