You are here

social_profile_fields.module in Open Social 8.6

The social profile fields module file.

File

modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module
View source
<?php

/**
 * @file
 * The social profile fields module file.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\profile\Entity\ProfileType;

/**
 * Implements hook_entity_field_access().
 */
function social_profile_fields_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {

  // By default, we return Switzerland.
  $access_result = AccessResult::neutral();
  if ($field_definition
    ->getTargetEntityTypeId() === 'profile') {
    $config = \Drupal::config('social_profile_fields.settings');
    $setting_name = $field_definition
      ->getTargetEntityTypeId() . '_' . $field_definition
      ->getTargetBundle() . '_' . $field_definition
      ->getName();
    $setting_value = $config
      ->get($setting_name);
    if (isset($setting_value) && !$setting_value) {

      // For the profile image field we have an exception. We'll fix it before
      // display.
      if ($setting_name === 'profile_profile_field_profile_image' && $operation === 'view') {
        $access_result = AccessResult::neutral();
      }
      else {
        $access_result = AccessResult::forbidden();
      }
    }
  }
  return $access_result;
}

/**
 * Implements hook_social_user_name_display_suggestions().
 *
 * Adds the nickname as a possible display name.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function social_profile_fields_social_user_name_display_suggestions(AccountInterface $account) : array {
  $suggestions = [];

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('profile');
  if (!($user_profile = $storage
    ->loadByUser($account, 'profile', TRUE))) {
    return $suggestions;
  }
  if (_social_profile_fields_get_setting('profile_profile_field_profile_nick_name') && !$user_profile->field_profile_nick_name
    ->isEmpty()) {

    // Add the nickname with a low weight so it's shown before the full name.
    $suggestions['nickname'] = [
      'weight' => -100,
      'name' => $user_profile->field_profile_nick_name->value,
    ];
  }
  return $suggestions;
}

/**
 * Implements hook_social_user_name_display_suggestions_alter().
 *
 * If the first name or last name (or both) is not enabled then we alter the
 * display of the full_name suggestion.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function social_profile_fields_social_user_name_display_suggestions_alter(array &$suggestions, AccountInterface $account) {
  social_profile_fields_censor_full_name_suggestion($suggestions, $account);
}

/**
 * Implements hook_social_user_name_display_suggestions_alter().
 *
 * Ensures that the full name suggestion doesn't contain disabled fields.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function social_profile_fields_censor_full_name_suggestion(array &$suggestions, AccountInterface $account) {

  // If there's no full name for us to censor then we're done.
  if (empty($suggestions['full_name'])) {
    return;
  }
  $use_first_name = _social_profile_fields_get_setting('profile_profile_field_profile_first_name');
  $use_last_name = _social_profile_fields_get_setting('profile_profile_field_profile_last_name');

  // If both fields are enabled then there is nothing for us to censor.
  if ($use_first_name && $use_last_name) {
    return;
  }

  // The field that is enabled must be loaded and checked to be non-empty.

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('profile');
  $user_profile = $storage
    ->loadByUser($account, 'profile', TRUE);

  // If we don't have a user profile then someone else has probably found data
  // from elsewhere and we have no fields to use.
  if (!$user_profile) {
    return;
  }

  // If both fields are disabled then we can unset the suggestion and be done.
  // We do this only if there's a profile because otherwise we may throw away
  // someone else's full_name pulled from elsewhere.
  if (!$use_first_name && !$use_last_name) {
    unset($suggestions['full_name']);
    return;
  }

  // We know that only one field is active.
  // So check just the first name.
  if ($use_first_name) {
    $first_name = $user_profile
      ->get('field_profile_first_name')->value;

    // If the only field we're allowed to use is empty then we remove the
    // suggestion altogether and fall back to something else.
    if (empty($first_name)) {
      unset($suggestions['full_name']);
    }
    else {
      $suggestions['full_name']['name'] = $first_name;
    }
  }
  else {
    $last_name = $user_profile
      ->get('field_profile_last_name')->value;

    // If the only field we're allowed to use is empty then we remove the
    // suggestion altogether and fall back to something else.
    if (empty($last_name)) {
      unset($suggestions['full_name']);
    }
    else {
      $suggestions['full_name']['name'] = $last_name;
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function social_profile_fields_profile_view_alter(array &$build, EntityInterface $entity, EntityViewDisplay $display) {

  // Check if profile image is unset.
  if (!_social_profile_fields_get_setting('profile_profile_field_profile_image') && isset($display
    ->get('content')['field_profile_image'])) {

    // Load default data.
    $replacement_data = social_profile_get_default_image();

    /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $image */
    $image = $build['field_profile_image'][0]['#item'];

    // Time to override the data that going to be rendered.
    $image
      ->set('target_id', $replacement_data['id']);
    $image
      ->set('width', $replacement_data['width']);
    $image
      ->set('height', $replacement_data['height']);

    // Put replacement data back in the object that's about to be built.
    $build['field_profile_image'][0]['#item'] = $image;
  }
}

/**
 * Get the value for the key from the settings.
 *
 * @param string $key
 *   The field name to check.
 *
 * @return bool
 *   If the field is enabled or disabled.
 */
function _social_profile_fields_get_setting($key) {
  $config = \Drupal::config('social_profile_fields.settings');
  $setting_value = $config
    ->get($key);
  if (!isset($setting_value) || $setting_value == TRUE) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Remove user export plugins for fields which are disabled.
 *
 * @param array $plugins
 *   An array of all the existing plugin definitions, passed by reference.
 *
 * @see \Drupal\social_user_export\UserExportPluginManager
 */
function social_profile_fields_social_user_export_plugin_info_alter(array &$plugins) {

  /** @var \Drupal\profile\Entity\ProfileType $profile_type */
  foreach (ProfileType::loadMultiple() as $profile_type) {
    $type = $profile_type
      ->id();

    /** @var \Drupal\field\Entity\FieldConfig $field_config */
    foreach (\Drupal::service('social_profile_fields.helper')
      ->getProfileFields($type) as $field) {
      $config = \Drupal::config('social_profile_fields.settings');
      $setting_value = $config
        ->get($field['id']);
      if (isset($setting_value) && !$setting_value) {
        $plugin_ids_for_fields = \Drupal::service('social_profile_fields.helper')
          ->getUserExportPluginIdForField($field['id']);
        if (!empty($plugin_ids_for_fields)) {
          foreach ($plugin_ids_for_fields as $plugin_id) {
            if ($plugins[$plugin_id]) {
              unset($plugins[$plugin_id]);
            }
          }
        }
      }
    }
  }
}