You are here

activity_logger.module in Open Social 8

File

modules/custom/activity_logger/activity_logger.module
View source
<?php

/**
 * @file
 * Contains activity_logger.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\message\MessageTemplateInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;

/**
 * Implements hook_help().
 */
function activity_logger_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the activity_logger module.
    case 'help.page.activity_logger':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Used to log activities based on the message module') . '</p>';
      return $output;
    default:
  }
}

/**
 * Helper function to get the content entity data.
 *
 * @return array
 *   Entity content data.
 */
function _activity_logger_get_content_entities() {
  $entitity_type_manager = \Drupal::service('entity_type.manager');
  $options = [];
  foreach ($entitity_type_manager
    ->getDefinitions() as $entity_id => $entity_type) {
    if ($entity_type instanceof ContentEntityTypeInterface && $entity_id !== 'activity' && $entity_id !== 'message' && $entity_id !== 'message_template') {
      $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
      $config_entity_bundles = $entity_type_bundle_info
        ->getBundleInfo($entity_type
        ->id());
      foreach ($config_entity_bundles as $key => $value) {

        // Dot character in key names is not allowed in config, so we use "-".
        $options[$entity_id . '-' . $key] = $entity_type
          ->getLabel() . ': ' . $value['label'];
      }
    }
  }
  return $options;
}

/**
 * Implements hook_form_FORM_ID_alter() for message_template_form().
 */
function activity_logger_form_message_template_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\contact\ContactFormInterface $contact_form */
  $message_template = $form_state
    ->getFormObject()
    ->getEntity();
  $wrapper_id = 'edit-activity-entity-condition-ajax-wrapper';
  $config_entity_bundles = _activity_logger_get_content_entities();
  $form['activity_bundle_entities'] = [
    '#type' => 'select',
    '#required' => TRUE,
    '#multiple' => TRUE,
    '#title' => t('The entities that are affected for this message'),
    '#description' => t('Select a entity bundle type to for this message.'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_bundle_entities', NULL),
    '#options' => $config_entity_bundles,
    '#ajax' => [
      'callback' => '_activity_logger_form_message_template_entity_ajax_callback',
      'wrapper' => $wrapper_id,
    ],
  ];
  $form['activity_entity_condition_wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => $wrapper_id,
    ],
  ];
  $activity_bundle_entities = $form_state
    ->getValue('activity_bundle_entities');
  if (empty($activity_bundle_entities)) {
    $activity_bundle_entities = $message_template
      ->getThirdPartySetting('activity_logger', 'activity_bundle_entities', []);
  }
  $activity_entity_condition_options = \Drupal::service('plugin.manager.activity_entity_condition.processor');
  $activity_entity_condition_options = $activity_entity_condition_options
    ->getOptionsList($activity_bundle_entities);
  if (empty($activity_entity_condition_options)) {
    $activity_entity_condition_value = NULL;
  }
  else {
    $activity_entity_condition_value = $message_template
      ->getThirdPartySetting('activity_logger', 'activity_entity_condition', NULL);
  }
  $form['activity_entity_condition_wrapper']['activity_entity_condition'] = [
    '#type' => 'select',
    '#title' => t('The entity condition that are affected for this message'),
    '#description' => t('Select a entity condition for this message.'),
    '#default_value' => $activity_entity_condition_value,
    '#options' => $activity_entity_condition_options,
    '#access' => !empty($activity_entity_condition_options),
  ];
  $activity_actions = \Drupal::service('plugin.manager.activity_action.processor');
  $activity_actions = $activity_actions
    ->getOptionsList();
  $form['activity_action'] = [
    '#type' => 'select',
    '#required' => TRUE,
    '#title' => t('The activity actions for this message'),
    '#description' => t('Select a action for when to display this message.'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_action', NULL),
    '#options' => $activity_actions,
  ];
  $activity_recipient_manager = \Drupal::service('plugin.manager.activity_context.processor');
  $context_options = $activity_recipient_manager
    ->getOptionsList();
  $form['activity_context'] = [
    '#type' => 'select',
    '#required' => TRUE,
    '#title' => t('The activity context for this message'),
    '#description' => t('Select a context where to display this message.'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_context', NULL),
    '#options' => $context_options,
  ];
  $activity_recipient_manager = \Drupal::service('plugin.manager.activity_destination.processor');
  $destination_options = $activity_recipient_manager
    ->getOptionsList();
  $form['activity_destinations'] = [
    '#type' => 'select',
    '#title' => t('The activity destinations for this message'),
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#description' => t('Select destinations where to display this message.'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_destinations', NULL),
    // @TODO activity_creator allowed_values function overlap (should be plugin)
    '#options' => $destination_options,
  ];
  $form['activity_create_direct'] = [
    '#type' => 'checkbox',
    '#required' => FALSE,
    '#title' => t('Create activity items direct instead of in Queue'),
    '#description' => t('Select if items should be created directly instead of in the queue. Warning: performance implications!'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_create_direct', NULL),
  ];
  $form['#entity_builders'][] = 'activity_logger_form_message_template_form_builder';
  $form['activity_aggregate'] = [
    '#type' => 'checkbox',
    '#required' => FALSE,
    '#title' => t('Aggregate activity'),
    '#description' => t('Select if items should be aggregated instead of displaying separately.'),
    '#default_value' => $message_template
      ->getThirdPartySetting('activity_logger', 'activity_aggregate', NULL),
  ];

  // Add titles to output text fields for clarity.
  if (isset($form['text'][0])) {
    $form['text'][0]['#title'] = t('Output text for normal activities in activity stream');
  }
  if (isset($form['text'][1])) {
    $form['text'][1]['#title'] = t('Output text for aggregated items in activity stream');
  }
  if (isset($form['text'][2])) {
    $form['text'][2]['#title'] = t('Output text for email notifications');
  }
}

/**
 * Entity builder for the message type edit form with third party options.
 *
 * @see activity_logger_form_message_template_form_alter()
 */
function activity_logger_form_message_template_form_builder($entity_type, MessageTemplateInterface $message_template, &$form, FormStateInterface $form_state) {
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_bundle_entities', $form_state
    ->getValue('activity_bundle_entities'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_action', $form_state
    ->getValue('activity_action'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_context', $form_state
    ->getValue('activity_context'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_destinations', $form_state
    ->getValue('activity_destinations'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_create_direct', $form_state
    ->getValue('activity_create_direct'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_aggregate', $form_state
    ->getValue('activity_aggregate'));
  $message_template
    ->setThirdPartySetting('activity_logger', 'activity_entity_condition', $form_state
    ->getValue('activity_entity_condition'));
}

/**
 * Handles switching the entity condition selector.
 */
function _activity_logger_form_message_template_entity_ajax_callback(&$form, FormStateInterface $form_state) {
  return $form['activity_entity_condition_wrapper'];
}

Functions

Namesort descending Description
activity_logger_form_message_template_form_alter Implements hook_form_FORM_ID_alter() for message_template_form().
activity_logger_form_message_template_form_builder Entity builder for the message type edit form with third party options.
activity_logger_help Implements hook_help().
_activity_logger_form_message_template_entity_ajax_callback Handles switching the entity condition selector.
_activity_logger_get_content_entities Helper function to get the content entity data.