You are here

social_profile_fields.module in Open Social 8.3

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_user_format_name_alter().
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 */
function social_profile_fields_user_format_name_alter(&$name, $account) {

  // Can we use the fields?
  $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');
  $use_nick_name = _social_profile_fields_get_setting('profile_profile_field_profile_nick_name');
  $accountname = '';

  // Let's get the Nickname, First name Last name.

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('profile');
  if (!empty($storage)) {

    // Returns false.
    if ($user_profile = $storage
      ->loadByUser($account, 'profile', TRUE)) {

      // First try to set it from the nick_name.
      $accountname = $use_nick_name ? $user_profile
        ->get('field_profile_nick_name')->value : '';

      // If that fails, fallback to old behaviour with first/last name.
      if ($accountname == '') {
        $first_name = $use_first_name ? $user_profile
          ->get('field_profile_first_name')->value : '';
        $last_name = $use_last_name ? $user_profile
          ->get('field_profile_last_name')->value : '';
        $accountname = $first_name . ' ' . $last_name;
        $accountname = trim($accountname);
      }
    }
  }

  // If all fails, we now fall back to the drupal username.
  $name = $accountname !== '' ? $accountname : $account
    ->getUsername();
}

/**
 * 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]);
            }
          }
        }
      }
    }
  }
}