You are here

function message_ui_entity_update in Message UI 8

Same name and namespace in other branches
  1. 7 message_ui.module \message_ui_entity_update()

Implements hook_entity_update().

Submit handler for updating the arguments number.

When a message template is been edited, there could be a change in the arguments of the message - added or removed. If this has been defined, we need to update the arguments of the other messages. This will be achieved by in two steps: 1. Load an instance of the message from the same template 2. Count the number of the arguments and if there is a difference between the number of the arguments from the old message to the current one - create a batch or a queue and update the messages.

File

./message_ui.module, line 260
Contains Drupal\message_ui\message_ui.module.

Code

function message_ui_entity_update(EntityInterface $entity) {
  $type = $entity
    ->getEntityType()
    ->id();
  if ($type != 'message_template') {
    return FALSE;
  }
  $query = \Drupal::entityQuery('message');
  $result = $query
    ->condition('template', $entity
    ->getTemplate())
    ->range(0, 1)
    ->sort('mid', 'DESC')
    ->execute();

  // There is no messages of this template.
  if (empty($result)) {
    return FALSE;
  }
  $keys = array_keys($result);
  $message = Message::load(reset($keys));
  $new_arguments = MessageArgumentsWorker::getArguments($entity
    ->getTemplate());
  $old_arguments_number = count($message
    ->getArguments());
  $new_arguments_number = count($new_arguments);
  $message_ui_settings_config = \Drupal::config('message_ui.settings');
  $how_to_act = $message_ui_settings_config
    ->get('update_tokens.how_to_act');
  $update['when_added'] = $old_arguments_number < $new_arguments_number && $how_to_act == 'update_when_added';
  $update['when_removed'] = $old_arguments_number > $new_arguments_number && $how_to_act == 'update_when_removed';
  if (!($update['when_added'] || $update['when_removed'])) {
    return FALSE;
  }
  $item_to_process = $message_ui_settings_config
    ->get('update_tokens.number_items');
  $how_to_update = $message_ui_settings_config
    ->get('update_tokens.how_to_update');
  if ($how_to_update == 'update_with_batch') {

    // Get all the messages.
    $query = \Drupal::entityQuery('message');
    $result = $query
      ->condition('template', $entity
      ->getTemplate())
      ->sort('mid', 'DESC')
      ->execute();
    $chunks = array_chunk(array_keys($result), $item_to_process);

    // @todo : Correct location for operations callback?
    $operations = [];
    foreach ($chunks as $chunk) {
      $operations[] = [
        '\\Drupal\\message_ui\\Plugin\\QueueWorker\\MessageArgumentsWorker::argumentsUpdate',
        [
          $chunk,
          $new_arguments,
        ],
      ];
    }

    // @todo : Correct location for finished callback?
    // Set the batch.
    $batch = [
      'operations' => $operations,
      'finished' => '\\Drupal\\message_ui\\Plugin\\QueueWorker\\MessageArgumentsWorker::messageArgumentsUpdate',
      'title' => t('Updating the messages arguments.'),
      'init_message' => t('Start process messages.'),
      'progress_message' => t('Processed @current out of @total.'),
      'error_message' => t('Example Batch has encountered an error.'),
    ];
    batch_set($batch);
    batch_process('admin/structure/messages');
  }
  elseif ($how_to_update == 'update_with_item') {

    // Define the queue item data.
    $data = [
      'template' => $entity
        ->getTemplate(),
      'last_mid' => 0,
      'new_arguments' => $new_arguments,
      'item_to_process' => $item_to_process,
    ];

    // Set the queue worker.
    $queue = \Drupal::queue('message_ui_arguments');
    return $queue
      ->createItem($data);
  }
  return NULL;
}