Skip to content
Snippets Groups Projects
Commit d963a235 authored by Nitin Lama's avatar Nitin Lama :cowboy: 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 or the file's encoding is unsupported.
<?php
/**
* @file
* Install, update, and uninstall functions for the Entity update diff json module.
*/
/**
* Implements hook_schema().
*/
function entity_update_diff_json_schema() {
$schema['entity_update_diff_json'] = [
'description' => 'entity_update_diff_json storagemysql',
'fields' => [
'id' => [
'description' => 'Holds the id value',
'type' => 'serial',
'not null' => TRUE,
],
'entity_title' => [
'description' => 'entity_title',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'uid' => [
'description' => 'uid',
'type' => 'int',
'not null' => TRUE,
],
'user_name' => [
'description' => 'user_name',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'created' => [
'description' => 'created',
'not null' => TRUE,
'mysql_type' => 'timestamp',
],
'entity_id' => [
'description' => 'entity_id',
'type' => 'int',
'not null' => TRUE,
],
'entity_type' => [
'description' => 'entity_type',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'original' => [
'description' => 'original',
'type' => 'blob',
'size' => 'normal', // normal / big
'not null' => TRUE,
],
'updated' => [
'description' => 'updated',
'type' => 'blob',
'size' => 'normal', // normal / big
'not null' => TRUE,
],
'differences' => [
'description' => 'updated',
'type' => 'blob',
'size' => 'normal', // normal / big
'not null' => TRUE,
],
$schema['entity_update_diff_json'] = [
'description' => 'entity_update_diff_json storagemysql',
'fields' => [
'id' => [
'description' => 'Holds the id value',
'type' => 'serial',
'not null' => TRUE,
],
'entity_title' => [
'description' => 'entity_title',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'uid' => [
'description' => 'uid',
'type' => 'int',
'not null' => TRUE,
],
'user_name' => [
'description' => 'user_name',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'created' => [
'description' => 'created',
'not null' => TRUE,
'mysql_type' => 'timestamp',
],
'primary key' => ['id'],
];
'entity_id' => [
'description' => 'entity_id',
'type' => 'int',
'not null' => TRUE,
],
'entity_type' => [
'description' => 'entity_type',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'original' => [
'description' => 'original',
'type' => 'blob',
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
'updated' => [
'description' => 'updated',
'type' => 'blob',
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
'differences' => [
'description' => 'updated',
'type' => 'blob',
// Normal / big.
'size' => 'normal',
'not null' => TRUE,
],
],
'primary key' => ['id'],
];
return $schema;
return $schema;
}
/**
* Implements hook_install().
*/
function entity_update_diff_json_install() {
$connection = \Drupal::service('database');
$schema = $connection->schema();
$connection = \Drupal::service('database');
$schema = $connection->schema();
if (!$schema->tableExists('entity_update_diff_json')){
drupal_install_schema('entity_update_diff_json');
}
}
\ No newline at end of file
if (!$schema->tableExists('entity_update_diff_json')) {
drupal_install_schema('entity_update_diff_json');
}
}
<?php
use Drupal\Core\Access\AccessResult;
/**
* @file
* Contains entity_update_diff_json.module.
*/
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityInterface;
function entity_update_diff_json_entity_presave(\Drupal\Core\Entity\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);
/**
* 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);
if(!($in_array_entity)){
return;
}
if(is_null($entity->original)){
return;
}
if (!($in_array_entity)) {
return;
}
if (is_null($entity->original)) {
return;
}
$original_array = !is_null($entity->original) ? $entity->original->toArray() : [];
$changed_array = !is_null($entity) ? $entity->toArray() : [];
$original_array = !is_null($entity->original) ? $entity->original->toArray() : [];
$changed_array = !is_null($entity) ? $entity->toArray() : [];
$encode_orignal = json_encode($original_array);
$encode_changed = json_encode($changed_array);
$results = array();
foreach ($original_array as $key => $value) {
$result = array();
if($value != $changed_array[$key]) {
$result['key_name'] = $key;
$result['old_value'] = $value;
$result['new_value'] = $changed_array[$key];
array_push($results, $result);
}
$encode_orignal = json_encode($original_array);
$encode_changed = json_encode($changed_array);
$results = [];
foreach ($original_array as $key => $value) {
$result = [];
if ($value != $changed_array[$key]) {
$result['key_name'] = $key;
$result['old_value'] = $value;
$result['new_value'] = $changed_array[$key];
array_push($results, $result);
}
$diffs = json_encode($results);
$user = \Drupal\user\Entity\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'])
->values([
'id' => NULL,
'entity_title' => $entity->getTitle(),
'uid' => \Drupal::currentUser()->id(),
'user_name' => $user->get('name')->value,
'created' => NULL,
'entity_id' => $entity->id(),
'entity_type' => $entity->getEntityTypeId(),
'original' => $encode_orignal,
'updated' => $encode_changed,
'differences' => $diffs,
])
->execute();
}
$diffs = json_encode($results);
$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',
])
->values([
'id' => NULL,
'entity_title' => $entity->getTitle(),
'uid' => \Drupal::currentUser()->id(),
'user_name' => $user->get('name')->value,
'created' => NULL,
'entity_id' => $entity->id(),
'entity_type' => $entity->getEntityTypeId(),
'original' => $encode_orignal,
'updated' => $encode_changed,
'differences' => $diffs,
])
->execute();
}
......@@ -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,17 +32,17 @@ class EntityUpdateDiffJsonController extends ControllerBase
];
$rows = [];
foreach ($entity_rows as $key => $value) {
$rows[] = [
$value->entity_title,
$value->user_name,
$value->created,
$value->entity_id,
$value->entity_type,
$value->original,
$value->updated,
$value->differences,
];
foreach ($entity_rows as $value) {
$rows[] = [
$value->entity_title,
$value->user_name,
$value->created,
$value->entity_id,
$value->entity_type,
$value->original,
$value->updated,
$value->differences,
];
}
return [
......@@ -52,7 +52,13 @@ class EntityUpdateDiffJsonController extends ControllerBase
];
}
public function getEntityUpdateDiffJsonRows(){
/**
* 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');
$query->fields('eudj');
......@@ -62,4 +68,4 @@ class EntityUpdateDiffJsonController extends ControllerBase
return $results;
}
}
\ No newline at end of file
}
<?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}
*/
......@@ -16,7 +17,7 @@ class EntityUpdateDiffJsonForm extends FormBase {
return 'entity_update_diff_json_form';
}
/**
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
......@@ -24,11 +25,14 @@ class EntityUpdateDiffJsonForm extends FormBase {
'entity_update_diff_json_form.settings',
];
}
/**
* {@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,33 +43,41 @@ 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;
}
private function getEntityTypes(){
/**
* 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();
$entity_types_list = [];
foreach($entity_definitions as $entity_name => $entity_definition) {
$entity_types_list[$entity_name] = (string) $entity_definition->getLabel();
foreach ($entity_definitions as $entity_name => $entity_definition) {
$entity_types_list[$entity_name] = (string) $entity_definition->getLabel();
}
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();
$config->save();
$config = \Drupal::service('config.factory')->getEditable('entity_update_diff_json_form.settings');
$config->set('entity_types', $form_state->getValue('entity_types'))->save();
$config->save();
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment