Skip to content
Snippets Groups Projects
Commit d963a235 authored by Nitin Lama's avatar Nitin Lama 🤠 Committed by Ricardo Salvado
Browse files
parent 89725a99
No related branches found
No related tags found
1 merge request!1#3360183
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
<?php
/**
* @file
* Install, update, and uninstall functions for the Entity update diff json module.
*/
/**
* Implements hook_schema().
*/
......@@ -48,19 +53,22 @@ function entity_update_diff_json_schema() {
'original' => [
'description' => 'original',
'type' => 'blob',
'size' => 'normal', // normal / big
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
'updated' => [
'description' => 'updated',
'type' => 'blob',
'size' => 'normal', // normal / big
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
'differences' => [
'description' => 'updated',
'type' => 'blob',
'size' => 'normal', // normal / big
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
......
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\node\Entity\Node;
function entity_update_diff_json_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
/**
* @file
* Contains entity_update_diff_json.module.
*/
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_presave().
*/
function entity_update_diff_json_entity_presave(EntityInterface $entity) {
$config = \Drupal::service('config.factory')->getEditable('entity_update_diff_json_form.settings');
$entity_types = !is_null($config->get('entity_types')) ? $config->get('entity_types') : [];
$in_array_entity = in_array($entity->getEntityTypeId(), $entity_types);
......@@ -22,9 +29,9 @@ function entity_update_diff_json_entity_presave(\Drupal\Core\Entity\EntityInterf
$encode_orignal = json_encode($original_array);
$encode_changed = json_encode($changed_array);
$results = array();
$results = [];
foreach ($original_array as $key => $value) {
$result = array();
$result = [];
if ($value != $changed_array[$key]) {
$result['key_name'] = $key;
$result['old_value'] = $value;
......@@ -33,10 +40,12 @@ function entity_update_diff_json_entity_presave(\Drupal\Core\Entity\EntityInterf
}
}
$diffs = json_encode($results);
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$user = User::load(\Drupal::currentUser()->id());
$connection = \Drupal::service('database');
$result = $connection->insert('entity_update_diff_json')
->fields(['id', 'entity_title', 'uid', 'user_name', 'created', 'entity_id', 'entity_type', 'original', 'updated', 'differences'])
->fields(['id', 'entity_title', 'uid', 'user_name', 'created', 'entity_id',
'entity_type', 'original', 'updated', 'differences',
])
->values([
'id' => NULL,
'entity_title' => $entity->getTitle(),
......
......@@ -4,22 +4,22 @@ namespace Drupal\entity_update_diff_json\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Class EntityUpdateDiffJsonController
* Class of EntityUpdateDiffJsonController.
*
* @package Drupal\entity_update_diff_json\Controller
*/
class EntityUpdateDiffJsonController extends ControllerBase
{
class EntityUpdateDiffJsonController extends ControllerBase {
/**
* The table of entity update json diff.
*
* @return array
* A renderable array of table columns.
*/
public function list()
{
public function list() {
$entity_rows = $this->getEntityUpdateDiffJsonRows();
//dump($entity_rows);
$header = [
'entity_title' => $this->t('Entity Title'),
'user_name' => $this->t('Username'),
......@@ -32,7 +32,7 @@ class EntityUpdateDiffJsonController extends ControllerBase
];
$rows = [];
foreach ($entity_rows as $key => $value) {
foreach ($entity_rows as $value) {
$rows[] = [
$value->entity_title,
$value->user_name,
......@@ -52,6 +52,12 @@ class EntityUpdateDiffJsonController extends ControllerBase
];
}
/**
* Query to get entity update diff json results.
*
* @return array
* A renderable array of results.
*/
public function getEntityUpdateDiffJsonRows() {
$database = \Drupal::database();
$query = $database->select('entity_update_diff_json', 'eudj');
......
<?php
/**
* @file
* Contains \Drupal\entity_update_diff_json\Form\EntityUpdateDiffJsonForm.
*/
namespace Drupal\entity_update_diff_json\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a form for the configuration settings.
*/
class EntityUpdateDiffJsonForm extends FormBase {
/**
* {@inheritdoc}
*/
......@@ -25,10 +26,13 @@ class EntityUpdateDiffJsonForm extends FormBase {
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('entity_update_diff_json_form.settings');
$form['entity_types'] = array (
$form['entity_types'] = [
'#type' => 'checkboxes',
'#title' => ('Entity Types:'),
'#options' => $this->getEntityTypes(),
......@@ -39,16 +43,22 @@ class EntityUpdateDiffJsonForm extends FormBase {
],
'#description' => $this->t('Select entity types who will be stored in database'),
'#default_value' => $config->get('entity_types'),
);
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Select'),
'#button_type' => 'primary',
);
];
return $form;
}
/**
* Returns an array of relevant entity types.
*
* @return array
* An array of entity types.
*/
private function getEntityTypes() {
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_definitions = $entity_type_manager->getDefinitions();
......@@ -61,7 +71,9 @@ class EntityUpdateDiffJsonForm extends FormBase {
return $entity_types_list;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::service('config.factory')->getEditable('entity_update_diff_json_form.settings');
$config->set('entity_types', $form_state->getValue('entity_types'))->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment