You are here

class Node in Audit Log 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/Node.php \Drupal\audit_log\EventSubscriber\Node

Processes node entity events.

@package Drupal\audit_log\EventSubscriber

Hierarchy

Expanded class hierarchy of Node

1 string reference to 'Node'
audit_log.services.yml in ./audit_log.services.yml
audit_log.services.yml
1 service uses Node
audit_log.event_subscriber.node in ./audit_log.services.yml
Drupal\audit_log\EventSubscriber\Node

File

src/EventSubscriber/Node.php, line 12

Namespace

Drupal\audit_log\EventSubscriber
View source
class Node implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public function reactTo(AuditLogEventInterface $event) {
    $entity = $event
      ->getEntity();
    if ($entity
      ->getEntityTypeId() != 'node') {
      return FALSE;
    }
    $event_type = $event
      ->getEventType();

    /** @var \Drupal\node\NodeInterface $entity */
    $current_state = $entity
      ->isPublished() ? 'published' : 'unpublished';
    $previous_state = '';
    if (isset($entity->original)) {
      $previous_state = $entity->original
        ->isPublished() ? 'published' : 'unpublished';
    }
    $args = [
      '@title' => $entity
        ->getTitle(),
    ];
    if ($event_type == 'insert') {
      $event
        ->setMessage('@name was created.')
        ->setMessagePlaceholders([
        '@name' => $entity
          ->label(),
      ])
        ->setPreviousState(NULL)
        ->setCurrentState($current_state);
      return TRUE;
    }
    if ($event_type == 'update') {
      $event
        ->setMessage('@name was updated.')
        ->setMessagePlaceholders([
        '@name' => $entity
          ->label(),
      ])
        ->setPreviousState($previous_state)
        ->setCurrentState($current_state);
      return TRUE;
    }
    if ($event_type == 'delete') {
      $event
        ->setMessage('@name was deleted.')
        ->setMessagePlaceholders([
        '@name' => $entity
          ->label(),
      ])
        ->setPreviousState($previous_state)
        ->setCurrentState(NULL);
      return TRUE;
    }
    return FALSE;
  }

}

Members