Skip to content

Instantly share code, notes, and snippets.

@nidaismailshah
Created May 5, 2020 09:30
Show Gist options
  • Select an option

  • Save nidaismailshah/ababff26a1233ef95379a4e732d971a6 to your computer and use it in GitHub Desktop.

Select an option

Save nidaismailshah/ababff26a1233ef95379a4e732d971a6 to your computer and use it in GitHub Desktop.
Alter Label or other attributes in rendered entities in Acquia ContentHub 2.x. Label appears when entities are listed in Acquia Lift Campaign Builder. Register this as an event subscriber within a custom drupal 8 module.
<?php
namespace Drupal\<custom_module>\EventSubscriber\Cdf;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\CreateCdfEntityEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
*
* @package Drupal\<custom_module>\EventSubscriber\Cdf
*/
class ParagraphLabel implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::CREATE_CDF_OBJECT][] = ['onCreateCdf', -100];
return $events;
}
/**
* Actions on create CDF.
*
* @param \Drupal\acquia_contenthub\Event\CreateCdfEntityEvent $event
* The create CDF entity event.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function onCreateCdf(CreateCdfEntityEvent $event) {
$entity = $event->getEntity();
if ($entity->getEntityTypeId() == 'paragraph' && $entity->bundle() == <paragraph_bundle>) {
$cdf_list = $event->getCdfList();
foreach ($cdf_list as $cdf) {
if ($cdf->getType() == 'rendered_entity') {
$attribute = $cdf->getAttribute('label');
// Set the field name of the field you want to set as label.
$field = <field name>;
$attribute->setValue($entity->{$field}->value, $entity->language()->getId());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment