Commit 23baff24 authored by Andrii Chyrskyi's avatar Andrii Chyrskyi
Browse files

Merge pull request #3027 from goalgorilla/issue/3294681

Issue #3294681 by tBKoT: Allows to filter users by the Profile tag
parent 07324aed
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -17,4 +17,15 @@ function social_profile_views_data_alter(array &$data) {
      'id' => 'profile_entity_sortable',
    ],
  ];

  $data['profile__field_profile_profile_tag']['social_split_profile_terms'] = [
    'title' => t('Filter by profile tags'),
    'filter' => [
      'title' => t('Filter by profile tags'),
      'help' => t('Provide a filter for split profile tags'),
      'group' => t('Profile'),
      'field' => 'field_profile_profile_tag_target_id',
      'id' => 'social_split_profile_terms',
    ],
  ];
}
+150 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\social_profile\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a filter for filtering on taxonomy term references.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("social_split_profile_terms")
 */
class SocialSplitProfileTerms extends TaxonomyIndexTid {

  /**
   * The social profile tag service.
   *
   * @var \Drupal\social_profile\SocialProfileTagServiceInterface
   */
  protected $profileTagService;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $filter = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $filter->profileTagService = $container->get('social_profile.tag_service');
    $filter->entityTypeManager = $container->get('entity_type.manager');
    return $filter;
  }

  /**
   * {@inheritdoc}
   */
  protected function valueForm(&$form, FormStateInterface $form_state): void {
    parent::valueForm($form, $form_state);

    // Use only for profile tag field.
    if ($this->realField !== 'field_profile_profile_tag_target_id') {
      return;
    }

    $element = &$form['value'];

    // Do not render field if no options or not active.
    if (
      !$this->profileTagService->isActive() ||
      !$this->profileTagService->hasContent() ||
      empty($element['#options'])
    ) {
      $element = [];
      return;
    }

    // Render all value if split tag disabled.
    if (!$this->profileTagService->allowSplit() && !empty($element['#options'])) {
      $term_ids = [];
      $element['#type'] = 'select2';
      $options = &$element['#options'];
      foreach ($options as $option) {
        $term_ids[] = array_keys($option->option)[0];
      }
      $options = $this->profileTagService->getTermOptionNames($term_ids);
      return;
    }

    // Get selected values.
    $identifier = $this->options['expose']['identifier'];
    $default_value = $form_state->getUserInput()[$identifier] ?? [];

    // Wrapper.
    $element = [];

    // Get the main categories.
    $categories = $this->profileTagService->getCategories();
    foreach ($categories as $tid => $category) {
      $field_name = 'profile_tagging_' . $this->profileTagService->tagLabelToMachineName($category);

      // Get the corresponding items.
      $options = $this->profileTagService->getChildrens($tid);

      // Display parent item in the tags list.
      if ($this->profileTagService->useCategoryParent()) {
        $options = [$tid => $category] + $options;
      }

      // Only add a field if the category has any options.
      if (count($options) > 0) {
        $element[$field_name] = [
          '#type' => 'select2',
          '#title' => $category,
          '#multiple' => TRUE,
          '#value' => $default_value,
          '#options' => $options,
          '#name' => $identifier,
        ];
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateExposed(&$form, FormStateInterface $form_state): void {
    if (
      $this->realField !== 'field_profile_profile_tag_target_id' ||
      !$this->profileTagService->allowSplit()
    ) {
      parent::validateExposed($form, $form_state);
      return;
    }

    $profile_tag_values = [];
    $identifier = $this->options['expose']['identifier'];

    // Get the main categories.
    $categories = $this->profileTagService->getCategories();

    // Get form values.
    $form_values = $form_state->getValues();
    foreach ($categories as $tid => $category) {
      $field_name = 'profile_tagging_' . $this->profileTagService->tagLabelToMachineName($category);
      if (isset($form_values[$field_name])) {
        $profile_tag_values += $form_values[$field_name];
        unset($form_values[$field_name]);
      }
    }
    $form_values[$identifier] = $profile_tag_values;
    $form_state->setValues($form_values);
    parent::validateExposed($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags(): array {
    return array_merge(parent::getCacheTags(), ['config:social_profile.settings']);
  }

}
+47 −0
Original line number Diff line number Diff line
@@ -5,11 +5,13 @@ dependencies:
    - core.entity_view_mode.profile.name
    - field.storage.profile.field_profile_first_name
    - field.storage.profile.field_profile_last_name
    - taxonomy.vocabulary.profile_tag
  module:
    - group
    - profile
    - social_profile
    - social_user
    - taxonomy
    - user
    - views_bulk_operations
id: user_admin_people
@@ -1248,6 +1250,51 @@ display:
          entity_type: user
          entity_field: login
          plugin_id: date
        social_split_profile_terms:
          id: social_split_profile_terms
          table: profile__field_profile_profile_tag
          field: social_split_profile_terms
          relationship: profile
          group_type: group
          admin_label: ''
          plugin_id: social_split_profile_terms
          operator: or
          value: { }
          group: 1
          exposed: true
          expose:
            operator_id: social_split_profile_terms_op
            label: 'Profile tags'
            description: ''
            use_operator: false
            operator: social_split_profile_terms_op
            operator_limit_selection: false
            operator_list: { }
            identifier: profile_tags
            required: false
            remember: false
            multiple: true
            remember_roles:
              authenticated: authenticated
            reduce: 0
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: { }
            group_items: { }
          reduce_duplicates: 1
          type: select
          limit: true
          vid: profile_tag
          hierarchy: 1
          error_message: 1
      sorts:
        created:
          id: created
+58 −0
Original line number Diff line number Diff line
views.view.user_admin_people:
  expected_config: { }
  update_actions:
    add:
      dependencies:
        config:
          - taxonomy.vocabulary.profile_tag
        module:
          - taxonomy
      display:
        default:
          display_options:
            filters:
              social_split_profile_terms:
                id: social_split_profile_terms
                table: profile__field_profile_profile_tag
                field: social_split_profile_terms
                relationship: profile
                group_type: group
                admin_label: ''
                plugin_id: social_split_profile_terms
                operator: or
                value: { }
                group: 1
                exposed: true
                expose:
                  operator_id: social_split_profile_terms_op
                  label: 'Profile tags'
                  description: ''
                  use_operator: false
                  operator: social_split_profile_terms_op
                  operator_limit_selection: false
                  operator_list: { }
                  identifier: profile_tags
                  required: false
                  remember: false
                  multiple: true
                  remember_roles:
                    authenticated: authenticated
                  reduce: 0
                is_grouped: false
                group_info:
                  label: ''
                  description: ''
                  identifier: ''
                  optional: true
                  widget: select
                  multiple: false
                  remember: false
                  default_group: All
                  default_group_multiple: { }
                  group_items: { }
                reduce_duplicates: 1
                type: select
                limit: true
                vid: profile_tag
                hierarchy: 1
                error_message: 1
+14 −0
Original line number Diff line number Diff line
@@ -341,3 +341,17 @@ function social_user_update_11201(): string {
  // Output logged messages to related channel of update execution.
  return $update_helper->logger()->output();
}

/**
 * Allow to filter users by Profile tag(s).
 */
function social_user_update_11401(): string {
  /** @var \Drupal\update_helper\UpdaterInterface $update_helper */
  $update_helper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $update_helper->executeUpdate('social_user', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $update_helper->logger()->output();
}
Loading