You are here

class FlagListsService in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/FlagListsService.php \Drupal\flag_lists\FlagListsService

Class FlagForList service.

Handles search requests for flags and flaggings.

Performs flagging and unflaging operations. Most of it SWP (Stolen With Pride) from flag module.

Hierarchy

Expanded class hierarchy of FlagListsService

1 string reference to 'FlagListsService'
flag_lists.services.yml in ./flag_lists.services.yml
flag_lists.services.yml
1 service uses FlagListsService
flaglists in ./flag_lists.services.yml
Drupal\flag_lists\FlagListsService

File

src/FlagListsService.php, line 17

Namespace

Drupal\flag_lists
View source
class FlagListsService implements FlagListsServiceInterface {

  /**
   * The entity query manager injected into the service.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  private $entityQueryManager;

  /**
   * The current user injected into the service.
   *
   * @var Drupal\Core\Session\AccountInterface
   */
  private $currentUser;

  /**
   * The type Entity Type Manager injected into the service.
   *
   * @var Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * Constructor.
   *
   * @param Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query factory.
   * @param Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   */
  public function __construct(QueryFactory $entity_query, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
    $this->entityQueryManager = $entity_query;
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Get all Flag For List, i.e. all Flags tagged as available.
   *
   * @param string $entity_type
   *   The entity wanted entity type.
   * @param string $bundle
   *   The wanted bundle of the entity.
   *
   * @return Drupal\flag_lists\Entity\FlagForList[]
   *   An array of found Flag For Lists.
   */
  public function getAllFlagForList($entity_type = NULL, $bundle = NULL) {
    $query = $this->entityQueryManager
      ->get('flag_for_list');
    if ($entity_type != NULL) {
      $query
        ->condition('entity_type', $entity_type);
    }
    $ids = $query
      ->execute();
    $flags = $this
      ->getFlagForListByIds($ids);
    if (isset($bundle)) {
      $flags = array_filter($flags, function (FlagForList $flag) use ($bundle) {
        $bundles = $flag
          ->getApplicableBundles();
        return in_array($bundle, $bundles);
      });
    }
    return $flags;
  }

  /**
   * Get current user's Flagging Collections.
   *
   * @return Drupal\flag_lists\Entity\FlaggingCollection[]
   *   An array of current User's Flagging Collections.
   */
  public function getUsersFlaggingCollections() {
    $query = $this->entityQueryManager
      ->get('flagging_collection');
    $query
      ->condition('user_id', $this->currentUser
      ->id());
    $ids = $query
      ->execute();
    $flags = $this
      ->getFlaggingCollectionByIds($ids);
    return $flags;
  }

  /**
   * Get all Flagging Collections.
   *
   * @param string $type
   *   The type, bundle, of the Flagging Collection.
   *
   * @return Drupal\flag_lists\Entity\FlaggingCollection[]
   *   An array of the found Flagging Collections.
   */
  public function getAllFlaggingCollections($type = NULL) {
    $query = $this->entityQueryManager
      ->get('flagging_collection');
    if ($type != NULL) {
      $query
        ->condition('type', $type);
    }
    $ids = $query
      ->execute();
    $flags = $this
      ->getFlaggingCollectionByIds($ids);
    return $flags;
  }

  /**
   * Get a user's Flag For List, i.e. all Flags tagged as available.
   *
   * NOT used nor tested!
   *
   * @param Drupal\Core\Session\AccountInterface $account
   *   The wanted user's account.
   * @param string $entity_type
   *   The wanted entity type.
   * @param string $bundle
   *   The wanted bundle of the entity.
   *
   * @return Drupal\flag_lists\Entity\FlagForList[]
   *   An array of Flag For Lists that the account has access to.
   */
  public function getUsersFlagForList(AccountInterface $account, $entity_type = NULL, $bundle = NULL) {
    $flagForList = $this
      ->getAllFlagForList($entity_type, $bundle);
    $filteredFlagForList = [];
    foreach ($flagForList as $flag_id => $flag) {
      if ($flag
        ->actionAccess('flag', $account)
        ->isAllowed() || $flag
        ->actionAccess('unflag', $account)
        ->isAllowed()) {
        $filteredFlagForList[$flag_id] = $flag;
      }
    }
    return $filteredFlagForList;
  }

  /**
   * Get a Flag For List, i.e. the Flags tagged as available as templates.
   *
   * @param int $flag_id
   *   The template id to load.
   *
   * @return Drupal\flag_lists\Entity\FlagForList[]
   *   An array of Flag For Lists.
   */
  public function getFlagForListById($flag_id) {
    return $this->entityTypeManager
      ->getStorage('flag_for_list')
      ->load($flag_id);
  }

  /**
   * Get a Flagging Collection by Id.
   *
   * @param string $flag_id
   *   The flag Id of the Flagging Collection to load.
   *
   * @return Drupal\flag_lists\Entity\FlaggingCollection
   *   The loaded Flagging Collection.
   */
  public function getFlaggingCollectionById($flag_id) {
    return $this->entityTypeManager
      ->getStorage('flagging_collection')
      ->load($flag_id);
  }

  /**
   * Loads Flag For Lists by ids.
   *
   * @param int[] $ids
   *   The Ids of the Flag For Lists to load.
   *
   * @return Drupal\flag_lists\Entity\FlagForList[]
   *   The loaded Flag For Lists.
   */
  protected function getFlagForListByIds(array $ids) {
    return $this->entityTypeManager
      ->getStorage('flag_for_list')
      ->loadMultiple($ids);
  }

  /**
   * Loads Flagging Collections by ids.
   *
   * @param int[] $ids
   *   The Ids of the Flag For Lists to load.
   *
   * @return Drupal\flag_lists\Entity\FlaggingCollection
   *   The loaded Flagging Collections.
   */
  protected function getFlaggingCollectionByIds(array $ids) {
    return $this->entityTypeManager
      ->getStorage('flagging_collection')
      ->loadMultiple($ids);
  }

  /**
   * Load a Flagging Collection Type by Id.
   *
   * @param string $type_id
   *   The Id of the Flagging Collection Type to load.
   *
   * @return Drupal\flag_lists\Entity\FlaggingCollectionType
   *   The loaded Flagging Collection Type
   */
  public function getFlaggingCollectionTypeById($type_id) {
    return $this->entityTypeManager
      ->getStorage('flagging_collection_type')
      ->load($type_id);
  }

  /**
   * Get Flag For List ids.
   *
   * @param string $baseflag
   *   The base flag to use.
   * @param string $flag_list
   *   The flag list.
   * @param int $entity_id
   *   The entity id.
   *
   * @return int[]
   *   An array of ids for the Flag For Lists
   */
  public function getFlagListItemIds($baseflag, $flag_list = NULL, $entity_id = NULL) {
    $query = $this->entityQueryManager
      ->get('flag_list_item');
    $query
      ->condition('baseflag', $baseflag);
    if ($flag_list != NULL) {
      $query
        ->condition('flag_list', $flag_list);
    }
    if ($entity_id != NULL) {
      $query
        ->condition('entity_id', $entity_id);
    }
    $ids = $query
      ->execute();
    return $ids;
  }

  /**
   * Get Flag List Items.
   *
   * @param int[] $ids
   *   The Flag List Item ids to load.
   *
   * @return Drupal\flag_lists\Entity\FlagListItem[]
   *   An array of Flag List Items.
   */
  public function getFlagListItems(array $ids) {
    return $this->entityTypeManager
      ->getStorage('flag_list_item')
      ->loadMultiple($ids);
  }

  /**
   * Get Flaggging Collection ids by name.
   *
   * NOT used nor tested!
   *
   * @param string $relatedName
   *   The name of the Flagging Collection to load.
   *
   * @return int[]
   *   The Flagging Collection id
   */
  public function getFlaggingCollectionIdByName($relatedName) {
    $query = $this->entityQueryManager
      ->get('flagging_collection');

    // The name is actually the same as the relatedName.
    $query
      ->condition('relatedflag', $relatedName);
    $id = $query
      ->execute();
    return $id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagListsService::$currentUser private property The current user injected into the service.
FlagListsService::$entityQueryManager private property The entity query manager injected into the service.
FlagListsService::$entityTypeManager private property The type Entity Type Manager injected into the service.
FlagListsService::getAllFlagForList public function Get all Flag For List, i.e. all Flags tagged as available.
FlagListsService::getAllFlaggingCollections public function Get all Flagging Collections.
FlagListsService::getFlagForListById public function Get a Flag For List, i.e. the Flags tagged as available as templates.
FlagListsService::getFlagForListByIds protected function Loads Flag For Lists by ids.
FlagListsService::getFlaggingCollectionById public function Get a Flagging Collection by Id.
FlagListsService::getFlaggingCollectionByIds protected function Loads Flagging Collections by ids.
FlagListsService::getFlaggingCollectionIdByName public function Get Flaggging Collection ids by name.
FlagListsService::getFlaggingCollectionTypeById public function Load a Flagging Collection Type by Id.
FlagListsService::getFlagListItemIds public function Get Flag For List ids.
FlagListsService::getFlagListItems public function Get Flag List Items.
FlagListsService::getUsersFlagForList public function Get a user's Flag For List, i.e. all Flags tagged as available.
FlagListsService::getUsersFlaggingCollections public function Get current user's Flagging Collections.
FlagListsService::__construct public function Constructor.