You are here

social_topic.module in Open Social 8.2

The Social topic module.

File

modules/social_features/social_topic/social_topic.module
View source
<?php

/**
 * @file
 * The Social topic module.
 */
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

/**
 * Prepares variables for node templates.
 *
 * Default template: node.html.twig.
 *
 * Most themes use their own copy of node.html.twig. The default is located
 * inside "/core/modules/node/templates/node.html.twig". Look in there for the
 * full list of variables.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An array of elements to display in view mode.
 *   - node: The node object.
 *   - view_mode: View mode; e.g., 'full', 'teaser', etc.
 */
function social_topic_preprocess_node(array &$variables) {

  /* @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];
  if ($node
    ->getType() === 'topic') {
    $topic_type = $node
      ->get('field_topic_type');
    $topic_type_entities = $topic_type
      ->referencedEntities();
    if (count($topic_type_entities) === 1) {
      foreach ($topic_type_entities as $topic) {
        $variables['metadata'] = t('in @topic', [
          '@topic' => $topic
            ->link(),
        ]);

        // Set topic type link.
        $topic_type_url = Url::fromRoute('view.latest_topics.page_latest_topics', [
          'field_topic_type_target_id' => $topic
            ->id(),
        ]);
        $topic_type_link = Link::fromTextAndUrl($topic
          ->label(), $topic_type_url)
          ->toString();
        $variables['topic_type'] = $topic_type_link;
      }
    }
    else {
      $variables['metadata'] = NULL;
      $variables['topic_type'] = NULL;
    }
  }
}

/**
 * Implements hook_form_form_ID_alter().
 *
 * Enhance the exposed filter form of the topic overview.
 */
function social_topic_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // On user topics overview.
  if ($form['#id'] === 'views-exposed-form-topics-page-profile') {
    $form['type']['#options']['All'] = t('All types');
    $form['status']['#options'][0] = t('Unpublished');
    $form['status']['#options'][1] = t('Published');
    $account_uid = \Drupal::routeMatch()
      ->getParameter('user');
    $current_uid = \Drupal::currentUser()
      ->id();
    if ($account_uid !== $current_uid) {
      $form['status']['#access'] = FALSE;
    }

    // Enable the reset button.
    // @todo make sure the block content refreshes on submit as well (AJAX).
    $form['actions']['reset']['#access'] = TRUE;

    // @todo make sure exposed form filtering redirects to the proper view
    // page, when views is updated.
    $form['#action'] = '/user/' . $account_uid . '/topics';
  }

  // On group topics overview.
  if ($form['#id'] === 'views-exposed-form-group-topics-page-group-topics') {
    $group_from_route = _social_group_get_current_group();
    $current_user = \Drupal::currentUser();
    $membership = FALSE;
    $group_membership = $group_from_route
      ->getMember($current_user);
    if ($group_membership) {
      $membership = TRUE;
    }
    if (!empty($form['status'])) {
      $form['status']['#options']['All'] = t('All statuses');
      $form['status']['#options'][0] = t('Unpublished');
      $form['status']['#options'][1] = t('Published');

      // Only show the unpublished option when you are member of the group.
      // You can't place content in a group you are not a member of anyway.
      if (!$membership) {
        $form['status']['#access'] = FALSE;
      }
    }
    $form['type']['#options']['All'] = t('All types');

    // Get group from route.
    if (!empty($group_from_route)) {
      $group_id = $group_from_route
        ->id();
    }
    $form['actions']['reset']['#access'] = TRUE;

    // Make sure we redirect to the current group page.
    $form['#action'] = '/group/' . $group_id . '/topics';
  }
}

/**
 * Implements hook_block_view_alter().
 *
 * Add a title to the exposed filter block on the topics overview.
 */
function social_topic_block_view_alter(array &$build, BlockPluginInterface $block) {

  // @todo check out why this happens, is this is a views bug?
  if (isset($build['#plugin_id']) && $build['#plugin_id'] === 'views_exposed_filter_block:topics-page_profile') {
    $build['#configuration']['label'] = $build['#configuration']['views_label'];
  }
}

/**
 * Implements hook_views_query_alter().
 */
function social_topic_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view
    ->id() == 'topics' && $view
    ->getDisplay()->display['id'] == 'page_profile') {
    $account_uid = \Drupal::routeMatch()
      ->getParameter('user');
    $current_uid = \Drupal::currentUser()
      ->id();
    if ($view->exposed_raw_input['status'] == NODE_PUBLISHED || $account_uid !== $current_uid) {
      $query->where[1]['conditions'][] = [
        'field' => 'node_field_data.status',
        'value' => NODE_PUBLISHED,
        'operator' => '=',
      ];
    }
  }

  // Only show the unpublished option when you are member of the group.
  // You can't place content in a group you are not a member of anyway.
  if ($view
    ->id() == 'group_topics' && $view
    ->getDisplay()->display['id'] == 'page_group_topics') {
    $group_from_route = _social_group_get_current_group();
    $current_user = \Drupal::currentUser();
    $membership = FALSE;
    $group_membership = $group_from_route
      ->getMember($current_user);
    if ($group_membership) {
      $membership = TRUE;
    }

    // Needs 1 and NODE_PUBLISHED, because it can be TRUE ( default ) or "1"
    // (see form alter when published is selected).
    if ($view->exposed_raw_input['status'] == "1" || $view->exposed_raw_input['status'] == NODE_PUBLISHED || !$membership) {
      $query->where[1]['conditions'][] = [
        'field' => 'node_field_data_group_content_field_data.status',
        'value' => NODE_PUBLISHED,
        'operator' => '=',
      ];
    }
  }
}

Functions

Namesort descending Description
social_topic_block_view_alter Implements hook_block_view_alter().
social_topic_form_views_exposed_form_alter Implements hook_form_form_ID_alter().
social_topic_preprocess_node Prepares variables for node templates.
social_topic_views_query_alter Implements hook_views_query_alter().