You are here

class LikeDislikePermissions in Like & Dislike 8

Provides dynamic permissions for nodes of different types.

Hierarchy

Expanded class hierarchy of LikeDislikePermissions

File

src/LikeDislikePermissions.php, line 16

Namespace

Drupal\like_and_dislike
View source
class LikeDislikePermissions implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $configFactory;

  /**
   * The bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfoService;

  /**
   * Constructs a \Drupal\like_and_dislike\Form\SettingsForm object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
   *   The bundle info service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $bundle_info_service) {
    $this->entityTypeManager = $entity_type_manager;
    $this->configFactory = $config_factory;
    $this->bundleInfoService = $bundle_info_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('config.factory'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * Builds a list of like_and_dislike related permissions.
   *
   * @return array
   *   An associative array of permission names and descriptions.
   */
  public function buildPermissions() {
    $permissions = [];
    $enabled_entity_types = $this->configFactory
      ->get('like_and_dislike.settings')
      ->get('enabled_types');
    $vote_types = VoteType::loadMultiple();
    foreach ($enabled_entity_types as $entity_type_id => $bundles) {

      // The entity type has no bundles. Add entity type permission only.
      $this
        ->addLikeAndDislikePermission($permissions, $vote_types, $entity_type_id, $bundles);
    }
    return $permissions;
  }

  /**
   * Adds vote types permissions for given entity type and bundles.
   *
   * @param array &$permissions
   *   An array of created permissions.
   * @param \Drupal\votingapi\VoteTypeInterface[] $vote_types
   *   An array of voting types.
   * @param string $entity_type_id
   *   The entity type ID.
   * @param array $bundles
   *   An array of bundles. Empty in case entity has no bundles.
   */
  protected function addLikeAndDislikePermission(array &$permissions, array $vote_types, $entity_type_id, array $bundles) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id, FALSE);

    // The entity type has no bundles other than the default one.
    if (empty($bundles)) {

      /** @var \Drupal\votingapi\VoteTypeInterface $vote_type */
      foreach ($vote_types as $vote_type) {
        $permissions["add or remove {$vote_type->id()} votes on {$entity_type_id}"] = [
          'title' => $this
            ->t('%entity_type_name: add/remove %vote_type_name', [
            '%entity_type_name' => $entity_type
              ->getLabel(),
            '%vote_type_name' => $vote_type
              ->label(),
          ]),
        ];
      }
    }
    else {
      foreach ($bundles as $bundle) {
        $bundle_info = $this->bundleInfoService
          ->getBundleInfo($entity_type_id)[$bundle];

        /** @var \Drupal\votingapi\VoteTypeInterface $vote_type */
        foreach ($vote_types as $vote_type) {
          $permissions["add or remove {$vote_type->id()} votes on {$bundle} of {$entity_type_id}"] = [
            'title' => $this
              ->t('%entity_type (%bundle): add/remove %vote_type', [
              '%entity_type' => $entity_type
                ->getLabel(),
              '%vote_type' => $vote_type
                ->label(),
              '%bundle' => $bundle_info['label'],
            ]),
          ];
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LikeDislikePermissions::$bundleInfoService protected property The bundle info service.
LikeDislikePermissions::$configFactory protected property The configuration factory.
LikeDislikePermissions::$entityTypeManager protected property The entity type manager.
LikeDislikePermissions::addLikeAndDislikePermission protected function Adds vote types permissions for given entity type and bundles.
LikeDislikePermissions::buildPermissions public function Builds a list of like_and_dislike related permissions.
LikeDislikePermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LikeDislikePermissions::__construct public function Constructs a \Drupal\like_and_dislike\Form\SettingsForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.