You are here

public function LearningPathGroupOperationsLinks::renderActionsDropdown in Opigno Learning path 3.x

Prepare the render array to build the available LP action links.

Parameters

\Drupal\group\Entity\GroupInterface $group: The group to build links for.

Return value

array The render array to build the available LP action links.

File

src/LearningPathGroupOperationsLinks.php, line 74

Class

LearningPathGroupOperationsLinks
The LP action links service.

Namespace

Drupal\opigno_learning_path

Code

public function renderActionsDropdown(GroupInterface $group) : array {
  $gid = (int) $group
    ->id();
  $build = [
    '#theme' => 'opigno_learning_path_actions',
    '#actions' => [],
  ];
  $options = [
    'attributes' => [
      'class' => [
        'dropdown-item-text',
      ],
    ],
  ];
  $forum_tid = $group
    ->get('field_learning_path_forum')
    ->getString();
  $actions = [];
  if ($forum_tid) {
    $actions['forum.page'] = [
      'title' => $this
        ->t('Forum'),
      'params' => [
        'taxonomy_term' => $forum_tid,
      ],
    ];
  }
  $actions += [
    'tft.group' => [
      'title' => $this
        ->t('Documents'),
      'params' => [
        'group' => $gid,
      ],
    ],
    'opigno_learning_path.training' => [
      'title' => $this
        ->t('Results'),
      'params' => [
        'group' => $gid,
      ],
    ],
    'opigno_social.share_post_content' => [
      'title' => $this
        ->t('Share'),
      'params' => [
        'group' => $gid,
      ],
    ],
    'entity.group.edit_form' => [
      'title' => $this
        ->t('Edit'),
      'params' => [
        'group' => $gid,
      ],
    ],
  ];

  // Generate the list of actions, available for the current user.
  foreach ($actions as $route => $action) {
    $params = $action['params'] ?? [];
    $url = Url::fromRoute($route, $params, $options);
    if ($route !== 'opigno_social.share_post_content' && $url
      ->access()) {
      $build['#actions'][] = Link::fromTextAndUrl($action['title'], $url);
      continue;
    }

    // Add extra settings for the sharing link.
    if ($route !== 'opigno_social.share_post_content' && !$url
      ->access() || !$this->isSocialsEnabled || !($this->account
      ->hasPermission('share any content') || $group
      ->getMember($this->account))) {
      continue;
    }
    $url = $url
      ->toString();
    $share_options = $options;
    $share_options['attributes']['data-opigno-attachment-type'] = 'training';
    $share_options['attributes']['data-opigno-attachment-entity-type'] = 'group';
    $share_options['attributes']['data-opigno-post-attachment-id'] = $gid;
    $build['#actions'][] = Link::createFromRoute($action['title'], '<current>', [], $share_options);
    $build['#attached'] = [
      'library' => [
        'opigno_social/post_sharing',
      ],
      'drupalSettings' => [
        'opignoSocial' => [
          'shareContentUrl' => $url instanceof GeneratedUrl ? $url
            ->getGeneratedUrl() : $url,
        ],
      ],
    ];
  }
  return $build;
}