You are here

class UserBlock in Content Planner 8

Provides a user block for Content Planner Dashboard.

Plugin annotation


@DashboardBlock(
  id = "user_block",
  name = @Translation("User Widget")
)

Hierarchy

Expanded class hierarchy of UserBlock

File

src/Plugin/DashboardBlock/UserBlock.php, line 20

Namespace

Drupal\content_planner\Plugin\DashboardBlock
View source
class UserBlock extends DashboardBlockBase {

  /**
   * Builds the render array for a dashboard block.
   *
   * @return array
   *   The markup for the dashboard block.
   */
  public function build() {
    $config = $this
      ->getConfiguration();
    $users = $this
      ->getUsers($config);
    if ($users) {
      $user_data = [];
      foreach ($users as $user) {
        $roles = array_map(function ($role) {
          if ($role != 'authenticated') {

            /** @var \Drupal\user\RoleInterface $role_entity */
            $role_entity = $this->entityTypeManager
              ->getStorage('user_role')
              ->load($role);
            return $role_entity ? $role_entity
              ->label() : NULL;
          }
        }, $user
          ->getRoles());
        $user_data[] = [
          'name' => $user
            ->label(),
          'image' => UserProfileImage::generateProfileImageUrl($user, 'content_planner_user_block_profile_image'),
          'roles' => implode(', ', array_filter($roles)),
          'content_count' => $this
            ->getUserContentCount($user
            ->id()),
          'content_kalendertag_count' => $this
            ->getUserContentWorkflowCount($user
            ->id(), 'am_kalendertag_publizieren'),
          'content_draft_count' => $this
            ->getUserContentWorkflowCount($user
            ->id(), 'draft'),
        ];
      }
      return [
        '#theme' => 'content_planner_dashboard_user_block',
        '#users' => $user_data,
      ];
    }
    return [];
  }

  /**
   * Loads the users with roles set in config.
   *
   * @return \Drupal\user\Entity\User[]
   *   Array of loaded user entities.
   */
  protected function getUsers() {
    if (isset($this
      ->getConfiguration()['plugin_specific_config']['roles'])) {

      // Get configured roles.
      $configured_roles = $this
        ->getConfiguration()['plugin_specific_config']['roles'];
      $query = \Drupal::entityQuery('user');
      $query
        ->condition('roles', array_values($configured_roles), 'in');
      $query
        ->sort('access', 'desc');
      $result = $query
        ->execute();
      if ($result) {
        return User::loadMultiple($result);
      }
    }
    return [];
  }

  /**
   * Get content count for a given user.
   *
   * @param int $user_id
   *   The user id to load the content count for.
   *
   * @return int
   *   The content count for the given user id.
   */
  protected function getUserContentCount($user_id) {
    $query = \Drupal::database()
      ->select('node_field_data', 'nfd');
    $query
      ->fields('nfd', [
      'nid',
    ]);
    $query
      ->condition('nfd.uid', $user_id);
    $query
      ->countQuery();
    $result = $query
      ->execute();
    $result->allowRowCount = TRUE;
    $count = $result
      ->rowCount();
    if ($count) {
      return $count;
    }
    return 0;
  }

  /**
   * Get content count for a given user based on workflow status.
   *
   * @param int $user_id
   *   The user id the get the workflow count for.
   * @param string $moderation_state
   *   The moderation state the get the count for.
   *
   * @return int
   *   The content count for the given user and the given moderation state.
   */
  public function getUserContentWorkflowCount($user_id, $moderation_state) {
    $kanban_service = \Drupal::service('content_kanban.kanban_service');
    $filters = [
      'uid' => $user_id,
      'moderation_state' => $moderation_state,
    ];
    $nids = $kanban_service
      ->getEntityIdsFromContentModerationEntities('netnode', $filters);
    return count($nids);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigSpecificFormFields(FormStateInterface &$form_state, Request &$request, array $block_configuration) {
    $form = [];

    // Build Role selection box.
    $form['roles'] = $this
      ->buildRoleSelectBox($form_state, $request, $block_configuration);
    return $form;
  }

  /**
   * Build Role select box.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   * @param \Symfony\Component\HttpFoundation\Request|null $request
   *   The current request.
   * @param array $block_configuration
   *   The block configuration.
   *
   * @return array
   *   The roles checkboxes.
   */
  protected function buildRoleSelectBox(FormStateInterface &$form_state, Request &$request, array $block_configuration) {

    // Get Roles.
    $roles = Role::loadMultiple();
    $roles_options = [];
    foreach ($roles as $role_id => $role) {
      if (in_array($role_id, [
        'anonymous',
      ])) {
        continue;
      }
      $roles_options[$role_id] = $role
        ->label();
    }
    $default_value = isset($block_configuration['plugin_specific_config']['roles']) ? $block_configuration['plugin_specific_config']['roles'] : [];
    return [
      '#type' => 'checkboxes',
      '#title' => t('Which Roles to display'),
      '#description' => t('Select which Roles should be displayed in the block.'),
      '#required' => TRUE,
      '#options' => $roles_options,
      '#default_value' => $default_value,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DashboardBlockBase::$entityTypeManager protected property The route match.
DashboardBlockBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
DashboardBlockBase::getBasicConfigStructure public static function Get basic configuration structure for the block configuration.
DashboardBlockBase::getConfiguration public function Get Configuration passed in by Plugin Manager. Overrides DashboardBlockInterface::getConfiguration
DashboardBlockBase::getCustomConfigByKey protected function Get custom config.
DashboardBlockBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
DashboardBlockBase::getName public function Return the name of the block. Overrides DashboardBlockInterface::getName
DashboardBlockBase::isConfigurable public function Determines if the plugin is configurable. Overrides PluginBase::isConfigurable
DashboardBlockBase::submitSettingsForm public function Submit form handler. Overrides DashboardBlockInterface::submitSettingsForm
DashboardBlockBase::validateForm public function Validates teh plugin config form. Overrides DashboardBlockInterface::validateForm
DashboardBlockBase::__construct public function Constructs a new UserLoginBlock instance. Overrides PluginBase::__construct 2
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
UserBlock::build public function Builds the render array for a dashboard block. Overrides DashboardBlockBase::build
UserBlock::buildRoleSelectBox protected function Build Role select box.
UserBlock::getConfigSpecificFormFields public function Add additonal form elements specific to the Plugin. Overrides DashboardBlockBase::getConfigSpecificFormFields
UserBlock::getUserContentCount protected function Get content count for a given user.
UserBlock::getUserContentWorkflowCount public function Get content count for a given user based on workflow status.
UserBlock::getUsers protected function Loads the users with roles set in config.