fixed #1847 disabled roles input managed by lincense in role_delegation form element

This commit is contained in:
Bachir Soussi Chiadmi 2022-11-15 17:11:18 +01:00
parent 5bd907ec1e
commit aff162514b
3 changed files with 287 additions and 65 deletions

View File

@ -75,7 +75,6 @@ permissions:
- 'access user profiles' - 'access user profiles'
- 'add commerce_log commerce_order admin comment' - 'add commerce_log commerce_order admin comment'
- 'add flag lists' - 'add flag lists'
- 'administer commerce_license'
- 'administer commerce_order' - 'administer commerce_order'
- 'administer commerce_payment' - 'administer commerce_payment'
- 'administer commerce_subscription' - 'administer commerce_subscription'

View File

@ -45,6 +45,7 @@ dependencies:
- user - user
- user_email_verification - user_email_verification
- views_bulk_operations - views_bulk_operations
- views_conditional
- views_data_export - views_data_export
id: admin_users id: admin_users
label: 'Admin Users' label: 'Admin Users'
@ -2775,7 +2776,74 @@ display:
entity_field: access entity_field: access
plugin_id: field plugin_id: field
label: 'Last access' label: 'Last access'
exclude: false exclude: true
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: true
empty_zero: true
hide_alter_empty: true
click_sort_column: value
type: commerce_license_expiration
settings:
date_format: html_date
custom_date_format: d-m-Y
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
access_1:
id: access_1
table: users_field_data
field: access
relationship: none
group_type: group
admin_label: ''
entity_type: user
entity_field: access
plugin_id: field
label: 'Dernier accès année'
exclude: true
alter: alter:
alter_text: false alter_text: false
text: '' text: ''
@ -2818,7 +2886,7 @@ display:
click_sort_column: value click_sort_column: value
type: timestamp type: timestamp
settings: settings:
date_format: html_date date_format: html_year
custom_date_format: '' custom_date_format: ''
timezone: '' timezone: ''
group_column: value group_column: value
@ -2831,6 +2899,63 @@ display:
multi_type: separator multi_type: separator
separator: ', ' separator: ', '
field_api_classes: false field_api_classes: false
views_conditional_field:
id: views_conditional_field
table: views_conditional
field: views_conditional_field
relationship: none
group_type: group
admin_label: ''
plugin_id: views_conditional_field
label: 'Last access '
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: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
if: access_1
condition: eq
equalto: '1970'
then: never
then_translate: 1
or: '{{ access }}'
or_translate: 1
strip_tags: 0
role_expire_expiry_data: role_expire_expiry_data:
id: role_expire_expiry_data id: role_expire_expiry_data
table: users table: users
@ -2895,7 +3020,7 @@ display:
entity_type: commerce_license entity_type: commerce_license
entity_field: product_variation entity_field: product_variation
plugin_id: field plugin_id: field
label: 'Licensed product variation' label: License
exclude: false exclude: false
alter: alter:
alter_text: false alter_text: false
@ -3592,20 +3717,22 @@ display:
views_bulk_operations_bulk_form: views_bulk_operations_bulk_form views_bulk_operations_bulk_form: views_bulk_operations_bulk_form
name: name name: name
mail: mail mail: mail
operations: operations operations: mail
roles_target_id: roles_target_id roles_target_id: roles_target_id
field_member_type: field_member_type field_member_type: field_member_type
field_company: field_company field_company: field_company
field_showroom: field_showroom field_showroom: field_showroom
access: access
created: created created: created
field_memo: field_memo
verified: verified
status: status
record_id: record_id record_id: record_id
access: access
access_1: access_1
views_conditional_field: views_conditional_field
role_expire_expiry_data: role_expire_expiry_data role_expire_expiry_data: role_expire_expiry_data
product_variation: product_variation product_variation: product_variation
expires: product_variation expires: product_variation
verified: verified
status: status
field_memo: field_memo
default: '-1' default: '-1'
info: info:
views_bulk_operations_bulk_form: views_bulk_operations_bulk_form:
@ -3651,9 +3778,23 @@ display:
default_sort_order: asc default_sort_order: asc
align: '' align: ''
separator: '' separator: ''
empty_column: false empty_column: true
responsive: '' responsive: ''
field_showroom: field_showroom:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: true
responsive: ''
created:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
record_id:
sortable: false sortable: false
default_sort_order: asc default_sort_order: asc
align: '' align: ''
@ -3667,14 +3808,33 @@ display:
separator: '' separator: ''
empty_column: false empty_column: false
responsive: '' responsive: ''
created: access_1:
sortable: true sortable: false
default_sort_order: asc default_sort_order: asc
align: '' align: ''
separator: '' separator: ''
empty_column: false empty_column: false
responsive: '' responsive: ''
field_memo: views_conditional_field:
align: ''
separator: ''
empty_column: false
responsive: ''
role_expire_expiry_data:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: true
responsive: ''
product_variation:
sortable: false
default_sort_order: asc
align: ''
separator: '<br>'
empty_column: true
responsive: ''
expires:
sortable: false sortable: false
default_sort_order: asc default_sort_order: asc
align: '' align: ''
@ -3686,7 +3846,7 @@ display:
default_sort_order: asc default_sort_order: asc
align: '' align: ''
separator: '' separator: ''
empty_column: false empty_column: true
responsive: '' responsive: ''
status: status:
sortable: false sortable: false
@ -3695,33 +3855,12 @@ display:
separator: '' separator: ''
empty_column: false empty_column: false
responsive: '' responsive: ''
record_id: field_memo:
sortable: false sortable: false
default_sort_order: asc default_sort_order: asc
align: '' align: ''
separator: '' separator: ''
empty_column: false empty_column: true
responsive: ''
role_expire_expiry_data:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
product_variation:
sortable: false
default_sort_order: asc
align: ''
separator: '<br>'
empty_column: false
responsive: ''
expires:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: '' responsive: ''
override: true override: true
sticky: true sticky: true

View File

@ -1,11 +1,15 @@
<?php <?php
// use Drupal\Core\Form\FormBuilder; // use Drupal\Core\Form\FormBuilder;
use \Drupal\Core\Link; use \Drupal\Core\Link;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\EntityOwnerInterface;
function materio_commerce_form_alter(&$form, &$form_state, $form_id) { function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'commerce_checkout_flow_multistep_default') { if ($form_id == 'commerce_checkout_flow_multistep_default') {
if ($form["#step_id"] == "review") { if ($form["#step_id"] == "review") {
$t="t";
// $form['buttons']['continue']['#value'] = t('Desired Custom Text Here'); // $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
$form['actions']['next']['#value'] = t('Place your order'); $form['actions']['next']['#value'] = t('Place your order');
} }
@ -16,7 +20,6 @@ function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_stat
$currentUser = \Drupal::currentUser(); $currentUser = \Drupal::currentUser();
$roles = $currentUser->getRoles(); $roles = $currentUser->getRoles();
if (in_array("admin", $roles)){ if (in_array("admin", $roles)){
$t="t";
$form['type']['#default_value'] = 'materio_order_type'; $form['type']['#default_value'] = 'materio_order_type';
$form['type']['#disabled'] = true; $form['type']['#disabled'] = true;
@ -24,51 +27,132 @@ function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_stat
} }
} }
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) { function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$form_state, $form_id) {
if ($form_id !== 'user_form') return;
$mail = $form['account']['mail']['#default_value'];
$user_storage = \Drupal::entityTypeManager()->getStorage('user'); /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$users = $user_storage->loadByProperties(['mail' => $mail]); $form_object = $form_state->getFormObject();
$user = reset($users); // if (!($form_object instanceof EntityFormInterface)) {
// $roles = $user->getRoles(); // // We're only interested in forms for an entity.
// return;
// }
/** @var \Drupal\user\UserInterface $account */
$account = $form_state->getBuildInfo()['args'][0];
// $entity = $form_object->getEntity();
// $entity_type_id = $entity->getEntityTypeId();
// if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
// // We only act on entities that have an owner, and user entities.
// return;
// }
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license'); $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
$licenses = $license_storage->loadByProperties(['uid' => $user->id()]); $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
if ($licenses) { if ($licenses) {
$form['license'] = [
"#type" => 'fieldset',
"#title" => 'licenses'
];
foreach ($licenses as $lid => $license) { foreach ($licenses as $lid => $license) {
$licensed_role_id = $license->license_role->target_id;
$form["account"]["role_change"][$licensed_role_id]['#disabled'] = TRUE;
$form["account"]["role_change"][$licensed_role_id]['#default_value'] = TRUE;
$product_variation = $license->getPurchasedEntity(); $product_variation = $license->getPurchasedEntity();
$product = $product_variation->getProduct(); $product = $product_variation->getProduct();
$infos = [];
$infos[] = $product->getTitle();
$infos[] = $product_variation->getTitle();
$granted = $license->getGrantedTime(); $granted = $license->getGrantedTime();
$infos[] = "granted: " . date('d-m-Y', $granted);
$expires = $license->getExpiresTime(); $expires = $license->getExpiresTime();
$infos[] = "expires: " . ($expires === "0" ? 'never' : date('d-m-Y', $expires));
$license_edit_url = $license->toUrl(); $license_edit_url = $license->toUrl();
$license_link = Link::fromTextAndUrl('edit license', $license_edit_url); $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
$infos[] = $license_link->toString();
$order = $license->getOriginatingOrder(); $order = $license->getOriginatingOrder();
$order_url = $order->toUrl(); $order_url = $order->toUrl();
$order_state = $order->get('state')->first()->getValue()['value']; $order_state = $order->get('state')->first()->getValue()['value'];
$order_number = $order->get('order_number')->first()->getValue()['value']; $order_number = $order->get('order_number')->first()->getValue()['value'];
$order_link = Link::fromTextAndUrl("order " . $order_number . " (" . $order_state . ")", $order_url); $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
$infos[] = $order_link->toString(); '@num' => $order_number,
'@state' => $order_state
)), $order_url);
$form["account"]["role_change"][$licensed_role_id]['#description'] = t("This role is granted by a license. It cannot be removed manually.<br/> @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
'@product' => $product->getTitle(),
'@variation' => $product_variation->getTitle(),
'@granted' => date('d-m-Y', $granted),
'@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
'@license_link' => $license_link->toString(),
'@order_link' => $order_link->toString()
));
}
}
}
function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
if ($form_id !== 'user_form') return;
// WE USE FORM OBJECT TO GET THE ENTITY instead of followings
// $mail = $form['account']['mail']['#default_value'];
// $user_storage = \Drupal::entityTypeManager()->getStorage('user');
// $users = $user_storage->loadByProperties(['mail' => $mail]);
// $user = reset($users);
// $roles = $user->getRoles();
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state->getFormObject();
if (!($form_object instanceof EntityFormInterface)) {
// We're only interested in forms for an entity.
return;
}
$entity = $form_object->getEntity();
$entity_type_id = $entity->getEntityTypeId();
if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
// We only act on entities that have an owner, and user entities.
return;
}
$license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
$licenses = $license_storage->loadByProperties(['uid' => $entity->id()]);
if ($licenses) {
// $form['license'] = [
// "#type" => 'fieldset',
// "#title" => 'licenses'
// ];
foreach ($licenses as $lid => $license) {
$licensed_role_id = $license->license_role->target_id;
$form["role_change"]["widget"][$licensed_role_id]['#disabled'] = TRUE;
$form["role_change"]["widget"][$licensed_role_id]['#default_value'] = TRUE;
$product_variation = $license->getPurchasedEntity();
$product = $product_variation->getProduct();
$granted = $license->getGrantedTime();
$expires = $license->getExpiresTime();
$license_edit_url = $license->toUrl();
$license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
$order = $license->getOriginatingOrder();
$order_url = $order->toUrl();
$order_state = $order->get('state')->first()->getValue()['value'];
$order_number = $order->get('order_number')->first()->getValue()['value'];
$order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
'@num' => $order_number,
'@state' => $order_state
)), $order_url);
$form["role_change"]["widget"][$licensed_role_id]['#description'] = t("This role is granted by a license. It cannot be removed manually.<br/> @product: @variation | granted: @granted | expires: @expires | @license_link | @order_link", array(
'@product' => $product->getTitle(),
'@variation' => $product_variation->getTitle(),
'@granted' => date('d-m-Y', $granted),
'@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
'@license_link' => $license_link->toString(),
'@order_link' => $order_link->toString()
));
$form['license']['license_'.$lid] = [
"#type" => 'item',
"#description" => implode(" | ", $infos)
];
} }
} }