You are here

class FlagCount in Flag 8.4

Provides a Twig extension to get the flag count given a flag and flaggable.

Hierarchy

  • class \Drupal\flag\TwigExtension\FlagCount extends \Drupal\flag\TwigExtension\Twig_Extension

Expanded class hierarchy of FlagCount

1 string reference to 'FlagCount'
flag.services.yml in ./flag.services.yml
flag.services.yml
1 service uses FlagCount
flag.twig.count in ./flag.services.yml
Drupal\flag\TwigExtension\FlagCount

File

src/TwigExtension/FlagCount.php, line 12

Namespace

Drupal\flag\TwigExtension
View source
class FlagCount extends \Twig_Extension {

  /**
   * The flag count.
   *
   * @var \Drupal\flag\FlagCountManagerInterface
   */
  protected $flagCount;

  /**
   * Constructs \Drupal\flag\TwigExtension\FlagCount.
   *
   * @param \Drupal\flag\FlagCountManagerInterface $flag_count
   *   The flag count service.
   */
  public function __construct($flag_count) {
    if (func_num_args() == 5) {
      $flag_count = func_get_arg(4);
      if (!$flag_count instanceof FlagCountManagerInterface) {
        $flag_count = \Drupal::service('flag.count');
      }
    }
    $this->flagCount = $flag_count;
  }

  /**
   * Generates a list of all Twig functions that this extension defines.
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('flagcount', [
        $this,
        'count',
      ], [
        'is_safe' => [
          'html',
        ],
      ]),
    ];
  }

  /**
   * Gets a unique identifier for this Twig extension.
   */
  public function getName() {
    return 'flag.twig.count';
  }

  /**
   * Gets the number of flaggings for the given flag and flaggable.
   *
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag entity.
   * @param \Drupal\Core\Entity\EntityInterface $flaggable
   *   The flaggable entity.
   *
   * @return string
   *   The number of times the flaggings for the given parameters.
   */
  public function count(FlagInterface $flag, EntityInterface $flaggable) {
    $counts = $this->flagCount
      ->getEntityFlagCounts($flaggable);
    return empty($counts) || !isset($counts[$flag
      ->id()]) ? '0' : $counts[$flag
      ->id()];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagCount::$flagCount protected property The flag count.
FlagCount::count public function Gets the number of flaggings for the given flag and flaggable.
FlagCount::getFunctions public function Generates a list of all Twig functions that this extension defines.
FlagCount::getName public function Gets a unique identifier for this Twig extension.
FlagCount::__construct public function Constructs \Drupal\flag\TwigExtension\FlagCount.