rc-1.0.x-dev/modules/rc_opensocial/rc_opensocial.module

modules/rc_opensocial/rc_opensocial.module
<?php

/**
 * @file
 * Contains rc_opensocial.module.
 */

use ATDev\RocketChat\Users\AvatarFromDomain;
use Drupal\profile\Entity\Profile;
use Drupal\user\Entity\User;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_help().
 */
function rc_opensocial_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the rc_opensocial module.
    case 'help.page.rc_opensocial':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provide cutomization for Opensocial distribution.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function rc_opensocial_theme($existing, $type, $theme, $path) {

  $templates = [];

  // Define a template for RC OpenSocial User block.
  $templates['rc_os_user_block'] = [
    'render element' => 'children',
    'template' => 'rc-os-user-block',
    'path' => $path . '/templates',
    'variables' => rc_theme_base_variables(),
  ];

  // Define a template for RC OpenSocial Group block.
  $templates['rc_os_group_block'] = [
    'render element' => 'children',
    'template' => 'rc-os-group-block',
    'path' => $path . '/templates',
    'variables' => rc_theme_base_variables() + [
        'group' => NULL,
        'button_text' => NULL,
    ],
  ];

  return $templates;

}

/**
 * Allows a module to add a link in the account header block.
 *
 * @param array $context
 *   The context that was provided to the block rendering these links.
 *
 * @return array
 *   An associative array of items that should be added in the account header
 *   block. The key of the items should be a unique item name.
 *
 * @see \Drupal\social_user\Element\AccountHeaderElement
 */
function rc_opensocial_social_user_account_header_items(array $context) {
  // Uses an AccountHeaderElement to easily render the RC user block.
  // Getting the current user object.
  $userId = \Drupal::currentUser()->id();
  $user = User::load($userId);

  // Check if user has a rocket chat ID field.
  if ($user->field_rcid->value) {

    return [
      'rc_chat' => [
        '#type' => 'account_header_element',
        '#wrapper_attributes' => [
          'class' => ['rc-os-user'],
          'onclick' => 'openRcOsUserBlock()',
        ],
        '#title' => t('Chat'),
        '#url' => '',
        '#icon' => 'discussion',
      ],
    ];
  }

}

/**
 * Implements HOOK_ENTITY_presave().
 */
function rc_opensocial_profile_presave(Profile $profile) {

  // @todo Test and enhance.
  updateUserAvatarRemote($profile);
}

/**
 * @throws \Drupal\Core\TypedData\Exception\MissingDataException
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function updateUserAvatarRemote(object $profile) {

  $rcUsers = \Drupal::service('rc.user');

  // Get the user associated with the profile.
  $user = $profile->getOwner();
  if ($rcUsers->loginRcAdmin()) {

    // Add the profile image.
    /** @var \Drupal\image\Entity\ImageStyle $image_style */
    $image_style = ImageStyle::load('social_medium');

    if (!empty($profile->field_profile_image->entity)) {
      $image_url = $image_style->buildUrl($profile->field_profile_image->entity->getFileUri());
    }

    elseif ($default_image = social_profile_get_default_image()) {

      // Add default image.
      if (!empty($default_image['id'])) {
        $file = File::load($default_image['id']);
        $image_url = $image_style->buildUrl($file->getFileUri());
      }
    }

    if ($image_url) {
      $rcUser = new \ATDev\RocketChat\Users\User($user->field_rcid->value);

      $avatar = new AvatarFromDomain($image_url);

      $result = $rcUser->resetAvatar($avatar);
      if (!$result) {

        // Log the error.
        \Drupal::logger('rc_opensocial')->error($rcUser->getError());

        return;
      }

      return $result;
    }
  }
}

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

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