You are here

class HashCdfAttribute in Acquia Content Hub 8.2

Calculates a hash value of the entity and stores it as an attribute.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\HashCdfAttribute implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of HashCdfAttribute

1 file declares its use of HashCdfAttribute
HashCdfAttributeTest.php in tests/src/Unit/EventSubscriber/CdfAttributes/HashCdfAttributeTest.php
1 string reference to 'HashCdfAttribute'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses HashCdfAttribute
entity.hash.cdf.attribute in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\HashCdfAttribute

File

src/EventSubscriber/CdfAttributes/HashCdfAttribute.php, line 14

Namespace

Drupal\acquia_contenthub\EventSubscriber\CdfAttributes
View source
class HashCdfAttribute implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES][] = [
      'onPopulateAttributes',
      100,
    ];
    $events[AcquiaContentHubEvents::BUILD_CLIENT_CDF][] = [
      'onBuildClientCdf',
      -100,
    ];
    return $events;
  }

  /**
   * On populate attributes.
   *
   * @param \Drupal\acquia_contenthub\Event\CdfAttributesEvent $event
   *   CDF attributes event.
   *
   * @throws \Exception
   */
  public function onPopulateAttributes(CdfAttributesEvent $event) {
    $cdf = $event
      ->getCdf();
    $wrapper = $event
      ->getWrapper();
    $cdf
      ->addAttribute('hash', CDFAttribute::TYPE_STRING, $wrapper
      ->getHash());
  }

  /**
   * On Build ClientCdf.
   *
   * @param \Drupal\acquia_contenthub\Event\BuildClientCdfEvent $event
   *   The BuildClientCdfEvent object.
   *
   * @throws \Exception
   */
  public function onBuildClientCdf(BuildClientCdfEvent $event) {
    $cdf = $event
      ->getCdf();
    $data = $cdf
      ->toArray();
    unset($data['created'], $data['modified']);
    $cdf
      ->addAttribute('hash', CDFAttribute::TYPE_KEYWORD, sha1(json_encode($data)));
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HashCdfAttribute::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
HashCdfAttribute::onBuildClientCdf public function On Build ClientCdf.
HashCdfAttribute::onPopulateAttributes public function On populate attributes.