You are here

function comment_alter_get_changed_fields in Comment Alter 8

Same name and namespace in other branches
  1. 7 comment_alter.module \comment_alter_get_changed_fields()

Returns a table showing the differences committed with a particular comment.

Uses the 'Diff' module to actually generate the differences.

Parameters

\Drupal\comment\CommentInterface $comment: The comment object.

Return value

array Table showing the differences made by the comment on the parent entity.

1 call to comment_alter_get_changed_fields()
comment_alter_comment_view in ./comment_alter.module
Implements hook_ENTITY_TYPE_view().

File

./comment_alter.module, line 408
Allows to alter entities from comment form.

Code

function comment_alter_get_changed_fields(CommentInterface $comment) {
  $changed_fields = [];
  if (isset($comment->comment_alter) && isset($comment->comment_alter['new_vid'])) {
    $parent_entity = $comment
      ->getCommentedEntity();
    $entity_type = $parent_entity
      ->getEntityTypeId();
    $bundle = $parent_entity
      ->bundle();
    $entity_storage = \Drupal::entityTypeManager()
      ->getStorage($entity_type);
    $old_entity = $entity_storage
      ->loadRevision($comment->comment_alter['old_vid']);
    $new_entity = $entity_storage
      ->loadRevision($comment->comment_alter['new_vid']);
    $comment_alter_fields = comment_alter_get_alterable_fields($entity_type, $bundle);

    // Use diff module's function to generate the row-wise differences between
    // the two revisions.
    $container = \Drupal::getContainer();
    $entity_comparison = EntityComparisonBase::create($container);
    $fields = $entity_comparison
      ->compareRevisions($old_entity, $new_entity);

    // Build the diff rows for each field and append the field rows
    // to the changed field array as new and old values.
    $diff_rows = [];
    $filter = 'raw';
    foreach ($fields as $field_name => $field) {

      // Because of some core changes the format of $field_name changed. For
      // some entities it became 'EntityType.ActualFieldName' and for the rest
      // it was still the actual field name. Get the field name from both of
      // these cases.
      $field_name_array = explode('.', $field_name);
      $actual_field_name = $field_name_array[sizeof($field_name_array) - 1];
      if (isset($comment_alter_fields[$actual_field_name]) && !empty($field['#name'])) {
        $field_diff_rows = comment_alter_get_rows_diff($field['#states'][$filter]['#left'], $field['#states'][$filter]['#right']);

        // Hide the differences if user chooses to hide them.
        $parent_field = $parent_entity
          ->getFieldDefinition($actual_field_name);
        if ($parent_field
          ->getThirdPartySetting('comment_alter', 'comment_alter_hide', FALSE)) {
          $changes = t('Changes are hidden');
          if ($parent_entity
            ->getEntityTypeId() == 'node') {
            $url = Url::fromUserInput('/node/' . $parent_entity
              ->id() . '/revisions/view/' . $comment->comment_alter['old_vid'] . '/' . $comment->comment_alter['new_vid']);
            $changes = \Drupal\Core\Link::fromTextAndUrl(t('View Changes'), $url);
          }
          $changed_fields[] = [
            'name' => [
              'data' => [
                '#markup' => '<b>' . $field['#name'] . '</b>',
              ],
            ],
            'changes' => $changes,
          ];
          continue;
        }
        $line = 0;
        foreach ($field_diff_rows as $row) {
          $changed_fields[] = [
            'name' => $line ? '' : [
              'data' => [
                '#markup' => '<b>' . $field['#name'] . '</b>',
              ],
            ],
            'old' => [
              'data' => empty($row[1]['data']) ? '' : $row[1]['data'],
            ],
            'arrow' => [
              'data' => [
                '#markup' => '&Rightarrow;',
              ],
            ],
            'new' => [
              'data' => empty($row[3]['data']) ? '' : $row[3]['data'],
            ],
          ];
          $line++;
        }
      }
    }
  }
  if (!empty($changed_fields)) {
    return [
      '#type' => 'table',
      '#rows' => $changed_fields,
      '#attributes' => [
        'class' => [
          'comment-alter-diff',
        ],
      ],
    ];
  }
}