You are here

event_log_track_menu.module in Events Log Track 8

Same filename and directory in other branches
  1. 8.2 event_log_track_menu/event_log_track_menu.module

Logs menu CUD commands in the event_log_track module.

File

event_log_track_menu/event_log_track_menu.module
View source
<?php

/**
 * @file
 * Logs menu CUD commands in the event_log_track module.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_event_log_track_handlers().
 */
function event_log_track_menu_event_log_track_handlers() {
  $handlers = array();
  $handlers['menu'] = array(
    'title' => t('Menu'),
    'form_ids' => array(
      'menu_link_content_menu_link_content_form',
      'menu_link_content_menu_link_content_delete_form',
    ),
    'form_submit_callback' => 'event_log_track_menu_form_submit',
  );
  return $handlers;
}

/**
 * Event log callback for the user authentication event log.
 *
 * @return array
 *   Return an associative array to log data in database.
 */
function event_log_track_menu_form_submit(array &$form, FormStateInterface $form_state, $form_id) {
  $log = NULL;
  switch ($form_id) {
    case 'menu_link_content_menu_link_content_form':
      $menu = $form_state
        ->getValues();
      $get_title = $menu['title'][0]['value'];
      $get_path = $menu['link'][0]['uri'];
      $get_menu_parent = substr($menu['menu_parent'], 0, -1);
      $route_parameters = \Drupal::routeMatch()
        ->getParameter('menu_link_content');
      if (empty($route_parameters)) {

        // Insert Menu Link.
        $log = array(
          'operation' => 'link insert',
          'description' => t('%title, %path', array(
            '%title' => $get_title,
            '%path' => $get_path,
          )),
          'ref_char' => $get_menu_parent,
        );
      }
      else {

        // Update Menu Link.
        $log = array(
          'operation' => 'link update',
          'description' => t('%title (%id), %path', array(
            '%title' => $get_title,
            '%id' => $route_parameters
              ->get('id')->value,
            '%path' => $get_path,
          )),
          'ref_numeric' => $route_parameters
            ->get('id')->value,
          'ref_char' => $route_parameters
            ->getMenuName(),
        );
      }
      break;
    case 'menu_link_content_menu_link_content_delete_form':

      // Delete Menu Link.
      $route_parameters = \Drupal::routeMatch()
        ->getParameter('menu_link_content');
      $uri = $route_parameters
        ->get('link')->uri;
      $log = array(
        'operation' => 'link delete',
        'description' => t('%title (%id), %path', array(
          '%title' => $route_parameters
            ->getTitle(),
          '%id' => $route_parameters
            ->get('id')->value,
          '%path' => $uri,
        )),
        'ref_numeric' => $route_parameters
          ->get('id')->value,
        'ref_char' => $route_parameters
          ->getMenuName(),
      );
      break;
  }
  return $log;
}

/**
 * Implements hook_menu_insert().
 */
function event_log_track_menu_menu_insert($menu) {
  $log = array(
    'type' => 'menu',
    'operation' => 'insert',
    'description' => t('%title (%name)', array(
      '%title' => $menu
        ->get('label'),
      '%name' => $menu
        ->get('id'),
    )),
    'ref_char' => $menu
      ->get('id'),
  );
  event_log_track_insert($log);
}

/**
 * Implements hook_menu_update().
 */
function event_log_track_menu_menu_update($menu) {
  $log = array(
    'type' => 'menu',
    'operation' => 'update',
    'description' => t('%title (%name)', array(
      '%title' => $menu
        ->get('label'),
      '%name' => $menu
        ->get('id'),
    )),
    'ref_char' => $menu
      ->get('id'),
  );
  event_log_track_insert($log);
}

/**
 * Implements hook_menu_delete().
 */
function event_log_track_menu_menu_delete($menu) {
  $log = array(
    'type' => 'menu',
    'operation' => 'delete',
    'description' => t('%title (%name)', array(
      '%title' => $menu
        ->get('label'),
      '%name' => $menu
        ->get('id'),
    )),
    'ref_char' => $menu
      ->get('id'),
  );
  event_log_track_insert($log);
}

Functions

Namesort descending Description
event_log_track_menu_event_log_track_handlers Implements hook_event_log_track_handlers().
event_log_track_menu_form_submit Event log callback for the user authentication event log.
event_log_track_menu_menu_delete Implements hook_menu_delete().
event_log_track_menu_menu_insert Implements hook_menu_insert().
event_log_track_menu_menu_update Implements hook_menu_update().