Commit 8153697d authored by mglaman's avatar mglaman Committed by bojanz
Browse files

Issue #2839060: Create an API for entity logs

parent f04be4d0
commerce_cart:
label: Cart
entity_type: commerce_order
commerce_order:
label: Order
entity_type: commerce_order
cart_entity_added:
category: commerce_cart
label: 'Added to cart'
template: '<p><em>{{ purchased_entity_label }}</em> added to the cart.</p>'
cart_item_removed:
category: commerce_cart
label: 'Removed from cart'
template: '<p><em>{{ purchased_entity_label }}</em> removed from the cart.</p>'
order_placed:
category: commerce_order
label: 'Order placed'
template: '<p>The order was placed.</p>'
name: Commerce Log
type: module
description: 'Provides activity logs for Commerce entities.'
package: Commerce
core: 8.x
dependencies:
- user
<?php
/**
* @file
* Provides activity logs for Commerce entities.
*/
/**
* Implements hook_preprocess_commerce_order().
*/
function commerce_log_preprocess_commerce_order(&$variables) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $variables['elements']['#commerce_order'];
$variables['order']['activity'] = [
'#type' => 'view',
'#name' => 'commerce_activity',
'#display_id' => 'default',
'#arguments' => [$order->id(), 'commerce_order'],
'#embed' => TRUE,
'#title' => t('Order activity'),
];
}
services:
plugin.manager.commerce_log_template:
class: Drupal\commerce_log\LogTemplateManager
arguments: ['@module_handler', '@cache.discovery', '@plugin.manager.commerce_log_category']
plugin.manager.commerce_log_category:
class: Drupal\commerce_log\LogCategoryManager
arguments: ['@module_handler', '@cache.discovery']
commerce_log.cart_event_subscriber:
class: Drupal\commerce_log\EventSubscriber\CartEventSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
commerce_log.order_event_subscriber:
class: Drupal\commerce_log\EventSubscriber\OrderEventSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
langcode: en
status: true
dependencies:
module:
- commerce_log
- user
id: commerce_activity
label: Activity
module: views
description: ''
tag: ''
base_table: commerce_log
base_field: log_id
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: none
options: { }
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: some
options:
items_per_page: 5
offset: 0
style:
type: table
row:
type: fields
fields:
created:
id: created
table: commerce_log
field: created
relationship: none
group_type: group
admin_label: ''
label: Date
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: timestamp
settings:
date_format: medium
custom_date_format: ''
timezone: ''
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: commerce_log
entity_field: created
plugin_id: field
rendered_entity:
id: rendered_entity
table: commerce_log
field: rendered_entity
relationship: none
group_type: group
admin_label: ''
label: Message
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
view_mode: default
entity_type: null
entity_field: null
plugin_id: rendered_entity
uid:
id: uid
table: commerce_log
field: uid
relationship: none
group_type: group
admin_label: ''
label: User
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: target_id
type: author
settings: { }
group_column: target_id
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: commerce_log
entity_field: uid
plugin_id: field
filters: { }
sorts:
created:
id: created
table: commerce_log
field: created
relationship: none
group_type: group
admin_label: ''
order: DESC
exposed: false
expose:
label: ''
granularity: second
entity_type: commerce_log
entity_field: created
plugin_id: date
log_id:
id: log_id
table: commerce_log
field: log_id
relationship: none
group_type: group
admin_label: ''
order: DESC
exposed: false
expose:
label: ''
entity_type: commerce_log
entity_field: log_id
plugin_id: standard
title: 'Order logs'
header: { }
footer: { }
empty:
area_text_custom:
id: area_text_custom
table: views
field: area_text_custom
relationship: none
group_type: group
admin_label: ''
empty: true
tokenize: false
content: 'No log entries.'
plugin_id: text_custom
relationships: { }
arguments:
source_entity_id:
id: source_entity_id
table: commerce_log
field: source_entity_id
relationship: none
group_type: group
admin_label: ''
default_action: empty
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
entity_type: commerce_log
entity_field: source_entity_id
plugin_id: numeric
source_entity_type:
id: source_entity_type
table: commerce_log
field: source_entity_type
relationship: none
group_type: group
admin_label: ''
default_action: 'not found'
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
entity_type: commerce_log
entity_field: source_entity_type
plugin_id: string
display_extenders: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
tags: { }
block_1:
display_plugin: block
id: block_1
display_title: Block
position: 1
display_options:
display_extenders: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
tags: { }
<?php
namespace Drupal\commerce_log\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the log entity class.
*
* @ContentEntityType(
* id = "commerce_log",
* label = @Translation("Log"),
* label_singular = @Translation("log"),
* label_plural = @Translation("logs"),
* label_count = @PluralTranslation(
* singular = "@count log",
* plural = "@count logs",
* ),
* handlers = {
* "access" = "Drupal\commerce\EntityAccessControlHandler",
* "list_builder" = "Drupal\commerce_log\LogListBuilder",
* "storage" = "Drupal\commerce_log\LogStorage",
* "view_builder" = "Drupal\commerce_log\LogViewBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* },
* base_table = "commerce_log",
* entity_keys = {
* "id" = "log_id",
* "uuid" = "uuid",
* },
* )
*/
class Log extends ContentEntityBase implements LogInterface {
/**
* {@inheritdoc}
*/
public function getUserId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function getUser() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->getCategory()->getLabel() . ': ' . $this->getTemplate()->getLabel();
}
/**
* {@inheritdoc}
*/
public function getCategoryId() {
return $this->get('category_id')->value;
}
/**
* {@inheritdoc}
*/
public function getCategory() {
$log_category_manager = \Drupal::service('plugin.manager.commerce_log_category');
return $log_category_manager->createInstance($this->getCategoryId());
}
/**
* {@inheritdoc}
*/
public function getTemplateId() {
return $this->get('template_id')->value;
}
/**
* {@inheritdoc}
*/
public function getTemplate() {
$log_template_manager = \Drupal::service('plugin.manager.commerce_log_template');
return $log_template_manager->createInstance($this->getTemplateId());
}
/**
* {@inheritdoc}
*/
public function getSourceEntityId() {
return $this->get('source_entity_id')->value;
}
/**
* {@inheritdoc}
*/
public function getSourceEntityTypeId() {
return $this->get('source_entity_type')->value;
}
/**
* {@inheritdoc}
*/
public function getSourceEntity() {
return \Drupal::entityTypeManager()
->getStorage($this->getSourceEntityTypeId())
->load($this->getSourceEntityId());
}
/**
* {@inheritdoc}
*/
public function getParams() {
return $this->get('params')->first()->getValue();
}
/**
* {@inheritdoc}
*/
public function setParams(array $params) {
$this->set('params', $params);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/