qs_article-1.0.2/qs_articles.module

qs_articles.module
<?php

/**
 * @file
 * QS Articles.
 */

use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_theme().
 */
function qs_articles_theme($existing, $type, $theme, $path) {
  return [

    'qs_sticky_dfp_block' => [
      'variables' => [
        'data' => [],
      ],
    ],

    'qs_save_article_user_list' => [
      'variables' => [
        'user_image' => [],
        'total_value' => NULL,
        'total_value_m' => NULL,
      ],
    ],

    'qs_leave_comment' => [
      'variables' => [
        'data' => [],
      ],
    ],
    
    'lead_carousel_modal' => [
      'variables' => ['carousel_data' => NULL],
    ],

    'lead_carousel_popup' => [
      'variables' => [],
    ],

    'qs_display_video_popup' => [
      'variables' => [],
    ],

    'qs_art_block' => [
      'variables' => [
        'data' => [],
      ],
    ],
    'qs_more_content' => [
      'variables' => [
        'data' => [],
      ],
    ],
  ];
}

/**
 * Implements hooks_views_query_alter().
 */
function qs_articles_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  /*
   * '30' = 'Last month'
   * '90' = 'Last 3 months'
   * '180' = 'Last 6 months'
   * '365' =  'Last year'
   */
  $current_path = Drupal::service('path.current')->getPath();
  $output = explode("/", $current_path);
  if ($output[1] != 'admin') {
    $tids = [];
    if ($view->id() == 'author_article_blocks') {
      switch ($view->getDisplay()->display['id']) {
        case 'block_3':
        case 'block_5':
        case 'block_6':
        case 'page_1':
        case 'page_2':

          $user_alias = '/' . $output[1] . '/' . $output[2];
          $db = Drupal::database();
          $userpath = $db
            ->select('path_alias', 'p')
            ->fields('p', ['path'])
            ->condition('alias', $user_alias)
            ->execute()
            ->fetchField();
          if ($userpath === FALSE) {
            $userid = is_numeric($output[2]) ? $output[2] : '1';
          }
          else {
            $user_id = explode("/user/", $userpath);
            $userid = $user_id[1];
          }
          foreach ($query->where as &$condition_group) {
            foreach ($condition_group['conditions'] as &$condition) {
              if ($condition['field'] == 'node_field_data.uid') {
                $condition = [
                  'field' => 'node_field_data.uid',
                  'value' => $userid,
                  'operator' => 'IN',
                ];
              }

            }
          }
          break;
      }
    }
    if ($view->id() == 'total_views_count_article') {
      $config = Drupal::config('qs_articleblocks.adminsettings');
      $date_range = $date_range = strtotime('-30 days');
      if (!empty($config->get('date_range'))) {
        $date_range = $date_range = strtotime('-' . $config->get('date_range') . ' days');
      }
      if ($view->getDisplay()->display['id'] == 'block_1' || $view->getDisplay()->display['id'] == 'block_2' || $view->getDisplay()->display['id'] == 'block_5') {
        $node = Drupal::routeMatch()->getParameter('node');
        if ($node instanceof NodeInterface) {
          $field_category = [];
          if (!empty($node->get('field_category'))) {
            $field_category = $node->get('field_category');
          }
          foreach ($field_category as $value) {
            $tids[] = $value->target_id;
          }
        }
      }
      else {
        if (preg_match('/taxonomy\/term\/(\d+)/', $current_path, $matches)) {
          $tids = $matches[1];
        }
        else {
          parse_str($_SERVER['QUERY_STRING'], $term_names);
          if (isset($term_names['cat'])) {
            foreach ($term_names['cat'] as $term_name) {
              $termdata = taxonomy_term_load_multiple_by_name(str_replace("_", " ", $term_name), 'categories');
              $term = reset($termdata);
              if(!empty($term)){
                $tids[] = $term->id();
              }             
            }
          }
        }
      }

      if (!empty($tids)) {
        foreach ($query->where as &$condition_group) {
          foreach ($condition_group['conditions'] as &$condition) {
            // If this is the part of the query filtering on category term id,
            // chang the condition to filter on category term id.
            // As per Last month, last 3 month,last 6 month or last year.
            if ($tids) {
              if ($condition['field'] == 'node__field_category.field_category_target_id = :node__field_category_field_category_target_id') {
                $condition = [
                  'field' => 'node__field_category.field_category_target_id',
                  'value' => $tids,
                  'operator' => 'IN',
                ];
              }
              if ($condition['field'] == 'node__field_category.field_category_target_id = :node__field_category_field_category_target_id2') {
                $condition = [
                  'field' => 'node__field_category.field_category_target_id',
                  'value' => $tids,
                  'operator' => 'IN',
                ];
              }
              if ($condition['field'] == 'node__field_category.field_category_target_id = :node__field_category_field_category_target_id1') {
                $condition = [
                  'field' => 'node__field_category.field_category_target_id',
                  'value' => $tids,
                  'operator' => 'IN',
                ];
              }
            }
            if ($condition['field'] == 'node_field_data.nid') {
              $condition = [
                'field' => 'node_field_data.nid',
                'value' => $output[2],
                'operator' => '!=',
              ];
            }
            if ($condition['field'] == 'node_field_data.changed <= ***CURRENT_TIME***+2592000') {
              $condition = [
                'field' => 'node_field_data.changed',
                'value' => $date_range,
                'operator' => '>=',
              ];
            }
          }
        }
      }
    }

    if ($view->id() == 'most_view_article_list') {
      parse_str($_SERVER['QUERY_STRING'], $term_names);
      if (array_key_exists("cat", $term_names)) {
        foreach ($term_names['cat'] as $term_name) {
          $termdata = taxonomy_term_load_multiple_by_name(str_replace("_", " ", $term_name), 'categories');
          $term = reset($termdata);
          if(!empty($term)){
            $tids[] = $term->id();
          }        
        }
        if (count($tids) > 0) {
          foreach ($query->where as &$condition_group) {
            foreach ($condition_group['conditions'] as &$condition) {
              if ($condition['field'] == 'node__field_category.field_category_target_id') {
                $condition = [
                  'field' => 'node__field_category.field_category_target_id',
                  'value' => $tids,
                  'operator' => 'IN',
                ];
              }
            }
          }
        }
      }
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave() for node entities.
 */
function qs_articles_node_presave(NodeInterface $node) {
  switch ($node->getType()) {
    case 'article':
      $cuser = User::load(\Drupal::currentUser()->id());
      $node->set('field_update_user_email', $cuser->get('mail')->value);
      if (isset($node->get('field_article_site_section')->getValue()[0]) ){
        $tid = $node->get('field_article_site_section')->getValue()[0]['target_id'];
        $aliasManager = \Drupal::service('path_alias.manager');
        $alias = $aliasManager->getAliasByPath('/taxonomy/term/'.$tid);
        $node->set('field_dynamic_path', $alias);
      }
      elseif (isset($node->get('field_category')->getValue()[0]) ){
        $tid = $node->get('field_category')->getValue()[0]['target_id'];
        $aliasManager = \Drupal::service('path_alias.manager');
        $alias = $aliasManager->getAliasByPath('/taxonomy/term/'.$tid);
        $node->set('field_dynamic_path', $alias);
      }


    break;
  }
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function qs_articles_field_widget_entity_reference_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
  /** @var \Drupal\field\Entity\FieldConfig $field_definition */
  $field_definition = $context['items']->getFieldDefinition();
  $paragraph_entity_reference_field_name = $field_definition->getName();

  if ($paragraph_entity_reference_field_name == 'field_article_lead_carousel') {

    /* @see \Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget::formElement() */
    $widget_state = WidgetBase::getWidgetState($element['#field_parents'], $paragraph_entity_reference_field_name, $form_state);

    /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
    if (isset($widget_state['paragraphs'])) {
      $paragraph_instance = $widget_state['paragraphs'][$element['#delta']]['entity'];
      $paragraph_type = $paragraph_instance->bundle();
      // Determine which paragraph type is being embedded.
      if ($paragraph_type == 'embed_carousel') {

        $dependee_field_name = 'field_type';
        $selector = sprintf('select[name="%s[%d][subform][%s]"]', $paragraph_entity_reference_field_name, $element['#delta'], $dependee_field_name);

        // Dependent fields.
        $element['subform']['field_carousel_image']['#states'] = [
          'visible' => [
            $selector => ['value' => 'image'],
          ],
        ];

        $element['subform']['field_video_url']['#states'] = [
          'visible' => [
            $selector => ['value' => 'video'],
          ],
        ];

        $element['subform']['field_video_title']['#states'] = [
          'visible' => [
            $selector => ['value' => 'video'],
          ],
        ];
      }
    }
  }
}

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

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