acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/acquia_vwo_content.module
modules/acquia_vwo_content/acquia_vwo_content.module
<?php
/**
* @file
* Acquia VWO Content - module file.
*/
use Drupal\acquia_vwo_content\Service\Helper\Util;
use Drupal\acquia_vwo_content\Service\Helper\EntityHelper;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_insert().
*/
function acquia_vwo_content_entity_insert(EntityInterface $entity): void {
if ($entity instanceof EntityPublishedInterface && !$entity->isPublished()) {
// Do not process, entity is unpublished.
return;
}
\Drupal::service('acquia_vwo_content.export_content')
->exportEntity($entity);
}
/**
* Implements hook_entity_update().
*/
function acquia_vwo_content_entity_update(EntityInterface $entity): void {
if (!$entity instanceof EntityPublishedInterface) {
// Do not process, entity is not publishable.
return;
}
$originalEntity = $entity->original ?? NULL;
// Determine if the original and current entities are eligible for export.
$isOriginalEligible = \Drupal::service('acquia_vwo_content.entity_helper')->isEligibleForExport($originalEntity);
$isCurrentEligible = \Drupal::service('acquia_vwo_content.entity_helper')->isEligibleForExport($entity);
// If the entity is published, decide whether to export or delete.
if ($entity->isPublished()) {
if ($isOriginalEligible && !$isCurrentEligible) {
// The original entity was eligible but the current one is not,
// so delete the original entity from VWO.
\Drupal::getContainer()
->get('acquia_vwo_content.export_content')
->deleteEntity($originalEntity);
}
else {
// Export the current entity.
\Drupal::getContainer()
->get('acquia_vwo_content.export_content')
->exportEntity($entity);
}
}
else {
// If the entity is not published, delete the current entity.
\Drupal::getContainer()
->get('acquia_vwo_content.export_content')
->deleteEntity($entity);
}
}
/**
* Implements hook_entity_delete().
*/
function acquia_vwo_content_entity_delete(EntityInterface $entity): void {
\Drupal::service('acquia_vwo_content.export_content')
->deleteEntity($entity);
}
/**
* Implements hook_page_attachments_alter().
*/
function acquia_vwo_content_page_attachments_alter(array &$page): void {
Util::attachLibrariesForPerz($page);
}
/**
* Implements hook_form_alter().
*/
function acquia_vwo_content_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$form_object = $form_state->getFormObject();
// Ensure this is an entity form.
if (!$form_object instanceof EntityFormInterface) {
return;
}
// Retrieve the entity.
$entity = $form_object->getEntity();
if (!$entity) {
return;
}
$entity_type_definition = $entity->getEntityType();
$entity_type_id = $entity_type_definition->getBundleOf();
if ($entity_type_id) {
$bundle = $entity->id();
$configured_view_modes = Util::getConfiguredViewModes($entity_type_id, $bundle);
// If no configured view modes exist, exit early.
if (empty($configured_view_modes)) {
return;
}
Util::addVwoContentSettingsForm($form, $entity_type_id, $bundle, $configured_view_modes);
}
if ($entity instanceof ContentEntityInterface) {
Util::handleContentEntityForm($form, $entity, $form_id);
}
}
/**
* Form submit handler for Acquia VWO entity settings.
*/
function acquia_vwo_content_entity_settings_form_submit(array $form, FormStateInterface $form_state): void {
$form_object = $form_state->getFormObject();
// Ensure this is an entity form.
if (!$form_object instanceof EntityFormInterface) {
return;
}
// Retrieve the entity.
$node_type = $form_object->getEntity();
if (!$node_type) {
return;
}
// Get the entity type ID.
$entity_type_definition = $node_type->getEntityType();
$entity_type_id = $entity_type_definition->getBundleOf();
if (!$entity_type_id) {
return;
}
$bundle = $node_type->id();
$enable_vwo = $form_state->getValue('enable_vwo');
$entity_view_mode = $form_state->getValue('view_mode');
$render_role = $form_state->getValue('render_role');
$export_all = $form_state->getValue('export_all');
// Load configuration.
$config = \Drupal::service('config.factory')->getEditable(EntityHelper::ENTITY_CONFIG_NAME);
$entity_config = $config->get('entity_config');
// Update entity configuration.
Util::updateEntityConfig(
$entity_config,
$entity_type_id,
$bundle,
$enable_vwo ? $entity_view_mode : NULL,
$enable_vwo ? $render_role : NULL,
$enable_vwo ? $export_all : FALSE
);
// Save updated configuration.
$config->set('entity_config', $entity_config)->save();
}
/**
* Form submit handler for acquia_vwo_content_form_alter().
*/
function acquia_vwo_content_form_submit(array &$form, FormStateInterface $form_state): void {
// Retrieve the form object.
$form_object = $form_state->getFormObject();
// Ensure it's an entity form.
if (!$form_object instanceof EntityFormInterface) {
return;
}
// Retrieve the entity.
$entity = $form_object->getEntity();
if (!$entity instanceof ContentEntityInterface) {
return;
}
// Update export status if the field is present.
if ($form_state->hasValue('field_acquia_vwo_export')) {
\Drupal::service('acquia_vwo_content.acquia_vwo_storage')->setExportStatus(
$entity->uuid(),
$entity->getEntityTypeId(),
$entity->id(),
(bool) $form_state->getValue('field_acquia_vwo_export')
);
}
}
