paragraphs_bundles-1.0.x-dev/modules/paragraph_bundle_content/paragraph_bundle_content.module

modules/paragraph_bundle_content/paragraph_bundle_content.module
<?php

/**
 * @file
 * Paragraph Bundle Content.
 *
 * Filename:     paragraph_bundle_content.module
 * Website:      https://www.flashwebcenter.com
 * Description:  template.
 * Developer:    Alaa Haddad https://www.alaahaddad.com.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;

/**
 * Implements hook_help().
 */
function paragraph_bundle_content_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name === 'help.page.paragraph_bundle_content') {
    return paragraphs_bundles__helper_render_readme();
  }
  return NULL;
}

/**
 * Implements hook_preprocess_page() for page templates.
 */
function paragraph_bundle_content_preprocess_page(&$variables) {

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  if ($admin_context->isAdminRoute()) {
    $variables['#attached']['library'][] = "paragraph_bundle_content/paragraph-bundle-content-admin";
  }

  $route_match  = \Drupal::routeMatch();
  $node_publish = $route_match->getParameter('node');
  $node_preview = $route_match->getParameter('node_preview');
  $node         = $node_publish ?? $node_preview;

  if ($node instanceof Node && $node->bundle() == 'pb_content') {
    $variables['#attached']['library'][] = "paragraph_bundle_content/paragraph-bundle-content-base";

    // Unset left and right.
    unset($variables['page']['sidebar_first'], $variables['page']['sidebar_second']);

    $selected_regions = array_column($node->pb_display_disable_region->getValue(), 'value');

    foreach ($selected_regions as $x => $selected_region) {

      switch ($selected_region) {

        case 'popup_login_block':
          unset($variables['page']['popup_login_block']);
          break;

        case 'fixed_search_block':
          unset($variables['page']['fixed_search_block']);
          break;

        case 'header':
          unset($variables['page']['header']);
          break;

        case 'primary_menu':
          unset($variables['page']['primary_menu']);
          break;

        case 'primary_sidebar_menu':
          unset($variables['page']['primary_sidebar_menu']);
          break;

        case 'welcome_text':
          unset($variables['page']['welcome_text']);
          break;

        case 'highlighted':
          unset($variables['page']['highlighted']);
          break;

        case 'top_regions':
          unset($variables['page']['top_first'], $variables['page']['top_second'], $variables['page']['top_third']);
          break;

        case 'page_title':
          unset($variables['page']['page_title']);
          break;

        case 'breadcrumb':
          unset($variables['page']['breadcrumb']);
          break;

        case 'bottom_regions':
          unset($variables['page']['bottom_first'], $variables['page']['bottom_second'], $variables['page']['bottom_third'], $variables['page']['bottom_fourth']);
          break;

        case 'footer_regions':
          unset($variables['page']['footer_first'], $variables['page']['footer_second'], $variables['page']['footer_third']);
          break;

        case 'footer_menu':
          unset($variables['page']['footer_menu']);
          $variables['#attached']['library'][] = "paragraph_bundle_content/hide-footer-menu";
          break;

        case 'copyright':
          $variables['#attached']['library'][] = "paragraph_bundle_content/hide-copyright";
          break;
      }

    }

  }
}

/**
 * Implements hook_theme().
 */
function paragraph_bundle_content_theme($existing, $type, $theme, $path) {
  return [
    'node__pb_content' => [
      'base hook' => 'node',
    ],
    'field__pb_content__pb_content_reference_para_block' => [
      'base hook' => 'field',
      'template' => 'field--pb-content-reference-para-block',
      'path' => \Drupal::service('extension.list.module')->getPath('paragraph_bundle_content') . '/templates',
    ],
    'field__pb_content__pb_content_type_body' => [
      'base hook' => 'field',
      'template' => 'field--pb-content-type-body',
      'path' => \Drupal::service('extension.list.module')->getPath('paragraph_bundle_content') . '/templates',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for field templates.
 */
function paragraph_bundle_content_theme_suggestions_field_alter(array &$suggestions, array $variables) {
  // Define the fields we are targeting.
  $targetFields = [
    'pb_content_reference_para_block',
    'pb_content_type_body',
  ];

  // The content type we are targeting.
  $targetBundle = 'pb_content';

  // The field name from the current render array.
  $currentFieldName = $variables['element']['#field_name'] ?? '';

  // Check if the current field is one of the target fields and if the bundle
  // matches what we're targeting.
  if (in_array($currentFieldName, $targetFields) &&
      isset($variables['element']['#bundle']) &&
      $variables['element']['#bundle'] === $targetBundle) {
    // Add a suggestion for the current field and bundle.
    $suggestions[] = 'field__' . $targetBundle . '__' . $currentFieldName;
  }
}

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

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