templating-8.x-2.0/templating.module

templating.module
<?php

/**
 * @file
 * Contains templating.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\NodeInterface;
/**
 * Implements hook_help().
 */
function templating_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the templating module.
    case 'help.page.templating':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Template Manager') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements MODULE_preprocess().
 *
 * @see RenderTest::testDrupalRenderThemePreprocessAttached()
 */
/**
 * Implements hook_theme_suggestions_alter().
 */
function templating_theme_suggestions_node_alter(array &$suggestions, array $variables, $hook) {
    if (isset($variables['elements']['#entity_type'])) {
        $entity_name = $variables['elements']['#entity_type'];
        $entity = $variables['elements']['#'.$entity_name] ;
        if(is_object($entity)){
            $view_mode = $variables['elements']['#view_mode'] ;
            $bundle = $entity->bundle();
            $id= $entity->id();
            $activeThemeName = \Drupal::service('theme.manager')->getActiveTheme();
            $theme = $activeThemeName->getName();
            $suggestion_1 = $entity_name."__".$theme."_".$bundle."_".$view_mode ;
            $suggestion_2 = $entity_name."__".$theme."_".$bundle."_".$id."_".$view_mode ;
            $suggestions[] = $suggestion_1;
            $suggestions[] = $suggestion_2;
        }
        }


}
/**
 * Implements hook_theme_suggestions_alter().
 */
function templating_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables, $hook) {

    if (isset($variables['elements']['#entity_type'])) {
        $entity_name = $variables['elements']['#entity_type'];
        $entity = $variables['elements']['#'.$entity_name] ;
        if(is_object($entity)){
            $view_mode = $variables['elements']['#view_mode'] ;
            $bundle = $entity->bundle();
            $id= $entity->id();
            $activeThemeName = \Drupal::service('theme.manager')->getActiveTheme();
            $theme = $activeThemeName->getName();
            $suggestion_1 = $entity_name."__".$theme."_".$bundle."_".$view_mode ;
            $suggestion_2 = $entity_name."__".$theme."_".$bundle."_".$id."_".$view_mode ;
            $suggestions[] = $suggestion_1;
            $suggestions[] = $suggestion_2;
        }
        }
}
function templating_theme_suggestions_block_alter(&$suggestions, $variables) {
    //Override Block Type
    $content = $variables['elements']['content'];
    $activeThemeName = \Drupal::service('theme.manager')->getActiveTheme();
    $theme = $activeThemeName->getName();
    if (isset($content['#block_content']) && $content['#block_content'] instanceof \Drupal\block_content\BlockContentInterface) {
        $view_mode = $variables['elements']['content']['#view_mode'];
        $id = $content['#block_content']->id();
        $suggestions[] = 'block__'.$theme.'_'.$content['#block_content']->bundle()."_".$view_mode;
        $suggestions[] = 'block__'.$theme.'_'.$content['#block_content']->bundle()."_".$id."_".$view_mode;

    }
    if(isset($content['content'])
    && $content['content']['#block_content'] instanceof \Drupal\block_content\BlockContentInterface){
        $id = $content['content']['#block_content']->id();
        $view_mode = $variables['elements']['content']['content']['#view_mode'];
        $suggestions[] = 'block__'.$theme.'_'.$content['content']['#block_content']->bundle()."_".$view_mode;
        $suggestions[] = 'block__'.$theme.'_'.$content['content']['#block_content']->bundle()."_".$id."_".$view_mode;
    }
    if (!isset($content['#block_content'])){
        $plugin_id = trim($variables['elements']['#plugin_id']);
        $base_plugin_id = trim($variables['elements']['#base_plugin_id']);
        $suggestions[] = 'block__'.$theme.'_'.$plugin_id;
        $suggestions[] = 'block__'.$theme.'_'.$base_plugin_id;
    }

}
/**
 * Implements hook_views_pre_render().
 */
 function templating_preprocess_views_view(&$variables) {
    $service = \Drupal::service('templating.manager');
    $output = $service->getTemplateView($variables);
    if(!empty($output)){
            foreach( $output as $key=> $value){
                     $element = [
                          '#type' => 'inline_template',
                          '#template' => $value,
                           '#context' => [
                             $key => $variables[$key]
                          ]
                     ];
                     $variables['template_'.$key] = $element ;
       }
    }
 }

/**
 * Implements hook_theme_registry_alter().
 */
function templating_theme_registry_alter(&$theme_registry) {
    $theme = \Drupal::theme()->getActiveTheme();
    $config = \Drupal::config('system.theme');
    $default_theme =  $config->get('default');
    $current_theme =  $theme->getName();
    if($default_theme == $current_theme){

      $theme_registry['block']['path'] = \Drupal::service('extension.list.module')->getPath('templating') .'/templates/misc' ;
      $theme_registry['html']['path'] = \Drupal::service('extension.list.module')->getPath('templating') .'/templates/misc' ;
      $theme_registry['node']['path'] = \Drupal::service('extension.list.module')->getPath('templating') .'/templates/misc' ;
      $theme_registry['views_view']['path'] = \Drupal::service('extension.list.module')->getPath('templating') .'/templates/misc' ;

    }
}
function templating_preprocess_html(&$variables) {
     $service = \Drupal::service('templating.manager');
     $asset = $service->getAllAsset();
     $route = \Drupal::routeMatch()->getRouteObject();
     $is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route);
     if(!$is_admin){
        $variables['html_page'] = $service->htmlPage();
        $config = \Drupal::config('portotheme_style_switcher.theme_style');
        $content = $config->get('css');
        if($config && $config->get('css')){
           $variables['css_template'] = $config->get('css').$asset["css"] ;
        }else{
           $variables['css_template'] = $asset["css"] ;
        }
        $variables['js_template'] = $asset["js"] ;
        $variables['library_template'] = $service->generateLibrary();
     }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function templating_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if($form_id == 'node_templating_form'){
        $path='/admin/template/create?destination=/admin/templating';
        $response = new RedirectResponse($path, 302);
        $response->send();
        return;
  }
   if($form_id == 'node_templating_edit_form'){

       $form['field_templating_mode_view']['widget'][0]['value']['#attributes'] = ['readonly' => 'readonly'];
       $form['field_templating_theme']['widget'][0]['value']['#attributes'] = ['readonly' => 'readonly'];
       $form['field_templating_entity_type']['widget'][0]['value']['#attributes'] = ['readonly' => 'readonly'];
       $form['field_templating_bundle']['widget'][0]['value']['#attributes'] = ['readonly' => 'readonly'];
       $form['title']['widget'][0]['value']['#attributes'] = ['readonly' => 'readonly'];

   }
}

function templating_node_update(NodeInterface $node) {
  if ($node->getType() == 'templating') {
    $id = $node->id();
    // You should use the route instead of path.
    $url = Url::fromRoute('entity.node.edit_form', ['node' => $id])->toString();
    // Redirecting to edit.
    $response = new RedirectResponse($url);
    $response->send();
  }
}
/**
 * Implements hook_menu_local_tasks_alter().
 * Add tab with custom title and link only to pages of a selected content type
 */
function templating_menu_local_tasks_alter(&$data, $route_name) {

  //do nothing if this ain't a display page
  if($route_name !== 'entity.node.canonical') return;
  $service = \Drupal::service('templating.manager');

  //try to get the currently displayed node
  $node = \Drupal::routeMatch()->getParameter('node');
  $template_node = $service->getTemplatingByEntity($node);
  //check if this is a node of the types you want to handle
  if($template_node) {
      $nid = $template_node->nid->value;
      $data['tabs'][0]['template_edit'] = [
        '#theme' => 'menu_local_task',
        '#link' => [
          'title' => t('Edit Template'),
          //create a url object. here from a route, but can be Url object.
           'url' => Url::fromRoute('entity.node.edit_form', ['node' => $nid])
        ],
      ];
  }

}
function _templating_theme_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
            $services = \Drupal::service('templating.manager');
            $themes = $services->getThemeList();
            $defaultThemeName = \Drupal::config('system.theme')->get('default');
            $theme_options = [];
            foreach ($themes as $theme) {
                $theme_options[$theme] = $theme;
            }
            return $theme_options ;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc