You are here

public function GroupSubscribeFormatter::viewElements in Organic groups 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php, line 114

Class

GroupSubscribeFormatter
Plugin implementation for the OG subscribe formatter.

Namespace

Drupal\og\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // Cache by the OG membership state.
  $elements['#cache']['contexts'] = [
    'og_membership_state',
  ];
  $group = $items
    ->getEntity();
  $entity_type_id = $group
    ->getEntityTypeId();

  // $user = User::load($this->currentUser->id());
  $user = $this->entityTypeManager
    ->load($this->currentUser
    ->id());
  if ($group instanceof EntityOwnerInterface && $group
    ->getOwnerId() == $user
    ->id()) {

    // User is the group manager.
    $elements[0] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => [
        'title' => $this
          ->t('You are the group manager'),
        'class' => [
          'group',
          'manager',
        ],
      ],
      '#value' => $this
        ->t('You are the group manager'),
    ];
    return $elements;
  }
  if (Og::isMemberBlocked($group, $user)) {

    // If user is blocked, they should not be able to apply for
    // membership.
    return $elements;
  }
  if (Og::isMember($group, $user, [
    OgMembershipInterface::STATE_ACTIVE,
    OgMembershipInterface::STATE_PENDING,
  ])) {
    $link['title'] = $this
      ->t('Unsubscribe from group');
    $link['url'] = Url::fromRoute('og.unsubscribe', [
      'entity_type_id' => $entity_type_id,
      'group' => $group
        ->id(),
    ]);
    $link['class'] = [
      'unsubscribe',
    ];
  }
  else {

    // If the user is authenticated, set up the subscribe link.
    if ($user
      ->isAuthenticated()) {
      $parameters = [
        'entity_type_id' => $group
          ->getEntityTypeId(),
        'group' => $group
          ->id(),
      ];
      $url = Url::fromRoute('og.subscribe', $parameters);
    }
    else {

      // User is anonymous, link to user login and redirect back to here.
      $url = Url::fromRoute('user.login', [], [
        'query' => $this
          ->getDestinationArray(),
      ]);
    }

    /** @var \Drupal\Core\Access\AccessResult $access */
    if (($access = $this->ogAccess
      ->userAccess($group, 'subscribe without approval', $user)) && $access
      ->isAllowed()) {
      $link['title'] = $this
        ->t('Subscribe to group');
      $link['class'] = [
        'subscribe',
      ];
      $link['url'] = $url;
    }
    elseif (($access = $this->ogAccess
      ->userAccess($group, 'subscribe', $user)) && $access
      ->isAllowed()) {
      $link['title'] = $this
        ->t('Request group membership');
      $link['class'] = [
        'subscribe',
        'request',
      ];
      $link['url'] = $url;
    }
    else {
      $elements[0] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'title' => $this
            ->t('This is a closed group. Only a group administrator can add you.'),
          'class' => [
            'group',
            'closed',
          ],
        ],
        '#value' => $this
          ->t('This is a closed group. Only a group administrator can add you.'),
      ];
      return $elements;
    }
  }
  if (!empty($link['title'])) {
    $link += [
      'options' => [
        'attributes' => [
          'title' => $link['title'],
          'class' => [
            'group',
          ] + $link['class'],
        ],
      ],
    ];
    $elements[0] = [
      '#type' => 'link',
      '#title' => $link['title'],
      '#url' => $link['url'],
    ];
  }
  return $elements;
}