You are here

acquia_contenthub_unsubscribe.module in Acquia Content Hub 8.2

Drupal Module: Acquia Content Hub unsubscribe.

The experimental module provides form elements for disconnecting local entities from remote updates. The form elements are not accessible by default. Custom code is required to determine when to expose these form elements.

File

modules/acquia_contenthub_unsubscribe/acquia_contenthub_unsubscribe.module
View source
<?php

/**
 * @file
 * Drupal Module: Acquia Content Hub unsubscribe.
 *
 * The experimental module provides form elements for disconnecting local
 * entities from remote updates. The form elements are not accessible by
 * default. Custom code is required to determine when to expose these form
 * elements.
 */
use Drupal\acquia_contenthub_subscriber\SubscriberTracker;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function acquia_contenthub_unsubscribe_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // This is an entity form.
  if (!in_array('Drupal\\Core\\Entity\\EntityFormInterface', class_implements($form_state
    ->getFormObject()))) {
    return;
  }

  /** @var \Drupal\Core\Entity\EntityFormInterface $object */
  $object = $form_state
    ->getFormObject();
  $entity = $object
    ->getEntity();
  if (!$entity) {
    return;
  }

  // If no id assigned yet, it's not remote.
  if (!$entity
    ->id()) {
    return;
  }

  /** @var \Drupal\acquia_contenthub_subscriber\SubscriberTracker $tracker */
  $tracker = \Drupal::service('acquia_contenthub_subscriber.tracker');

  // Don't alter entities not tracked by the subscriber.
  if (!$tracker
    ->isTracked($entity
    ->uuid())) {
    return;
  }
  $status = $tracker
    ->getStatusByTypeId($entity
    ->getEntityTypeId(), $entity
    ->id());
  $status = $status === SubscriberTracker::AUTO_UPDATE_DISABLED ? TRUE : FALSE;
  $form['acquia_contenthub_subscriber_sync'] = [
    '#type' => 'checkbox',
    '#title' => t("Disable auto updating of this entity"),
    '#description' => t("Disabling auto updating will prevent this entity from being updated by Acquia ContentHub in the future."),
    '#default_value' => $status,
    '#weight' => 1000,
    '#access' => FALSE,
  ];
  if (isset($form['actions']['submit'])) {
    $form['actions']['submit']['#submit'][] = 'acquia_contenthub_subscriber_sync_state_submit';
  }
}

/**
 * Submit handler for dealing with contenthub auto updating status.
 *
 * @param array $form
 *   The form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current form state.
 *
 * @throws \Exception
 */
function acquia_contenthub_unsubscribe_sync_state_submit(array &$form, FormStateInterface $form_state) {
  if (!$form_state
    ->hasValue('acquia_contenthub_subscriber_sync')) {
    return;
  }

  /** @var \Drupal\Core\Entity\EntityFormInterface $object */
  $object = $form_state
    ->getFormObject();
  $entity = $object
    ->getEntity();
  if (!$entity) {
    return;
  }

  /** @var \Drupal\acquia_contenthub_subscriber\SubscriberTracker $tracker */
  $tracker = \Drupal::service('acquia_contenthub_subscriber.tracker');
  if ($form_state
    ->getValue('acquia_contenthub_subscriber_sync')) {
    $tracker
      ->setStatusByTypeId($entity
      ->getEntityTypeId(), $entity
      ->id(), SubscriberTracker::AUTO_UPDATE_DISABLED);
  }
  else {
    $tracker
      ->setStatusByTypeId($entity
      ->getEntityTypeId(), $entity
      ->id(), SubscriberTracker::QUEUED);

    // @todo re-enqueue the entity to be updated to the current version in the services.
  }
}

Functions

Namesort descending Description
acquia_contenthub_unsubscribe_form_alter Implements hook_form_alter().
acquia_contenthub_unsubscribe_sync_state_submit Submit handler for dealing with contenthub auto updating status.