-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PE-671 Show pages where an employee is a contact with bulk actions (#635
) * 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
1 parent
64103a2
commit b5c7850
Showing
12 changed files
with
1,300 additions
and
291 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
web/modules/custom/employees/src/Plugin/Action/ChangeContactAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
web/modules/custom/employees/src/Plugin/Action/RemoveContactAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
web/sites/default/config/block.block.westy_views_block__user_manager_block_1.yml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.