You are here

PostGroupBlock.php in Open Social 8.6

File

modules/social_features/social_post/src/Plugin/Block/PostGroupBlock.php
View source
<?php

namespace Drupal\social_post\Plugin\Block;

use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Provides a 'PostGroupBlock' block.
 *
 * @Block(
 *  id = "post_group_block",
 *  admin_label = @Translation("Post on group block"),
 * )
 */
class PostGroupBlock extends PostBlock {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, $entityTypeManager, $currentUser, $formBuilder, ModuleHandler $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entityTypeManager, $currentUser, $formBuilder, $moduleHandler);
    $this->entityType = 'post';
    $this->bundle = 'post';
    $this->formDisplay = 'group';
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    $group = _social_group_get_current_group();
    if (is_object($group)) {
      if ($group
        ->hasPermission('add post entities in group', $account) && $account
        ->hasPermission("add post entities")) {
        $membership = $group
          ->getMember($account);
        $context = [];
        if (!empty($membership)) {
          $group_content = $membership
            ->getGroupContent();
          $context = [
            'group_content' => $group_content,
          ];
        }
        return \Drupal::entityTypeManager()
          ->getAccessControlHandler($this->entityType)
          ->createAccess($this->bundle, $account, $context, TRUE);
      }
    }

    // By default, the block is not visible.
    return AccessResult::forbidden()
      ->setCacheMaxAge(0);
  }

}

Classes

Namesort descending Description
PostGroupBlock Provides a 'PostGroupBlock' block.