Skip to content

Commit

Permalink
PE-671 Show pages where an employee is a contact with bulk actions (#635
Browse files Browse the repository at this point in the history
)

* PE-671 Show pages where an employee is a contact with bulk actions

* PE-671 Add revision log messages, improved Reports display of user names, and use layout builder instead of block placement
  • Loading branch information
RichardDavies authored Jan 7, 2025
1 parent 64103a2 commit b5c7850
Show file tree
Hide file tree
Showing 12 changed files with 1,300 additions and 291 deletions.
100 changes: 100 additions & 0 deletions web/modules/custom/employees/src/Plugin/Action/ChangeContactAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\employees\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Access\AccessResult;

/**
* Publish content
*
* @Action(
* id = "employees_change_contact_action",
* label = @Translation("Change contact (custom action)"),
* type = "node",
* confirm = FALSE,
* )
*/
class ChangeContactAction extends ViewsBulkOperationsActionBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if ($entity === NULL || !$entity->hasField('field_contact')) {
return $this->t('Invalid entity or configuration.');
}

$contacts = $entity->get('field_contact');

// Look through all contacts to find the old contact and replace it with the new contact
foreach ($contacts as $contact) {
$user_id = $contact->getValue()['target_id'];
if ($user_id == $this->configuration['old_user_id']) {
$contact->setValue($this->configuration['new_user_id']);

// Get the first and last name of the old and new contacts for the revision log message
$from_user = \Drupal::entityTypeManager()->getStorage('user')->load($this->configuration['old_user_id']);
$from_first_name = $from_user->get('field_first_name')->value;
$from_last_name = $from_user->get('field_last_name')->value;

$to_user = \Drupal::entityTypeManager()->getStorage('user')->load($this->configuration['new_user_id']);
$to_first_name = $to_user->get('field_first_name')->value;
$to_last_name = $to_user->get('field_last_name')->value;

// Make this change a new revision
if($entity->hasField('revision_log'))
$entity->revision_log = "Bulk operation: Changed contact $from_first_name $from_last_name to $to_first_name $to_last_name";
$entity->setNewRevision(TRUE);
$entity->setRevisionCreationTime(\Drupal::time()->getRequestTime());
$entity->setRevisionUserId(\Drupal::currentUser()->id());

$entity->save();
return $this->t('Contact successfully changed.');
}
}
}


public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$from_user_id = $this->context['arguments'][0];
$from_first_name = \Drupal::entityTypeManager()->getStorage('user')->load($from_user_id)->get('field_first_name')->value;
$from_last_name = \Drupal::entityTypeManager()->getStorage('user')->load($from_user_id)->get('field_last_name')->value;

$form['from_username'] = [
'#type' => 'markup',
'#markup' => '<p><strong>Change from:</strong> ' . "$from_first_name $from_last_name" . '</p>',
];

$form['old_user_id'] = [
'#type' => 'hidden',
'#value' => $from_user_id,
];

$form['new_user_id'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Change to'),
'#placeholder' => $this->t('Enter user name'),
'#target_type' => 'user',
// '#selection_settings' => ['filter' => ['access' => 'administer members']],
'#required' => TRUE,
'#tags' => FALSE,
// '#description' => $this->t('Use a comma to select multiple users.'),
];

return $form;
}


/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object->access('edit', $account, $return_as_object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Drupal\employees\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Access\AccessResult;

/**
* Publish content
*
* @Action(
* id = "employees_remove_contact_action",
* label = @Translation("Remove as contact (custom action)"),
* type = "node",
* confirm = FALSE,
* )
*/
class RemoveContactAction extends ViewsBulkOperationsActionBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if ($entity === NULL || !$entity->hasField('field_contact')) {
return $this->t('Invalid entity or configuration.');
}

$contacts = $entity->get('field_contact');

// Look through all contacts to find the right contact and remove it
foreach ($contacts as $contact) {
$user_id = $contact->getValue()['target_id'];
if ($user_id == $this->configuration['user_id']) {
$contacts->removeItem($contact->getName());

// Get the first and last name of the contact for the revision log message
$user = \Drupal::entityTypeManager()->getStorage('user')->load($this->configuration['user_id']);
$user_first_name = $user->get('field_first_name')->value;
$user_last_name = $user->get('field_last_name')->value;

// Make this change a new revision
if($entity->hasField('revision_log'))
$entity->revision_log = "Bulk operation: Removed contact $user_first_name $user_last_name";
$entity->setNewRevision(TRUE);
$entity->setRevisionCreationTime(\Drupal::time()->getRequestTime());
$entity->setRevisionUserId(\Drupal::currentUser()->id());

$entity->save();
return $this->t('Contact successfully removed.');
}
}
}


public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$user_id = $this->context['arguments'][0];
$user = \Drupal::entityTypeManager()->getStorage('user')->load($user_id);
$first_name = $user->get('field_first_name')->value;
$last_name = $user->get('field_last_name')->value;

$form['from_username'] = [
'#type' => 'markup',
'#markup' => "<p><strong>$first_name $last_name</strong> will be removed as a contact from the selected items.</p>",
];

$form['user_id'] = [
'#type' => 'hidden',
'#value' => $user_id,
];

return $form;
}


/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object->access('edit', $account, $return_as_object);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ dependencies:
- field.field.user.user.field_workspace
- field.field.user.user.user_picture
- image.style.thumbnail
- views.view.content_list
- views.view.my_groups
- views.view.user_manager
module:
- image
- layout_builder
- layout_builder_restrictions
- layout_discovery
- layout_library
- telephone
- user
Expand Down Expand Up @@ -465,6 +468,39 @@ third_party_settings:
third_party_settings: { }
weight: 6
additional: { }
e064305f-5997-46d1-b3c3-d59e7cab48ba:
uuid: e064305f-5997-46d1-b3c3-d59e7cab48ba
region: main
configuration:
id: 'views_block:user_manager-block_1'
label: Reports
label_display: visible
provider: views
context_mapping: { }
views_label: Reports
items_per_page: none
weight: 23
additional: { }
third_party_settings: { }
-
layout_id: layout_onecol
layout_settings:
label: ''
context_mapping: { }
components:
cd07515f-de8f-4780-a62f-3adf11ec7bb0:
uuid: cd07515f-de8f-4780-a62f-3adf11ec7bb0
region: content
configuration:
id: 'views_block:content_list-block_assigned_as_a_contact'
label: ''
label_display: visible
provider: views
context_mapping: { }
views_label: ''
items_per_page: none
weight: 0
additional: { }
third_party_settings: { }
layout_library:
enable: false
Expand Down
Loading

0 comments on commit b5c7850

Please sign in to comment.