Created
September 26, 2023 22:33
-
-
Save mordonez/6ff13faac67e75b89600995ddf16b2fd to your computer and use it in GitHub Desktop.
Drupal: Views Bulk Operations Action
This file contains hidden or 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
<?php | |
//src/Plugin/Action/SyncSendgridAddAction.php | |
namespace Drupal\offering_sync_sendgrid\Plugin\Action; | |
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\Core\StringTranslation\StringTranslationTrait; | |
/** | |
* Action description. | |
* | |
* @Action( | |
* id = "sync_sendgrid_add_action", | |
* label = @Translation("Subscribe User to Sendgrid List"), | |
* type = "user", | |
* requirements = { | |
* "_permission" = "administer users" | |
* } | |
* ) | |
* | |
*/ | |
class SyncSendgridAddAction extends ViewsBulkOperationsActionBase | |
{ | |
use StringTranslationTrait; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute($entity = NULL) | |
{ | |
$service = \Drupal::service('offering_sync_sendgrid.sync_sendgrid'); | |
$email = $entity->getEmail(); | |
$response = $service->addContactsToSendgrid([$email]); | |
if ($response) { | |
\Drupal::logger('offering_sync_sendgrid')->notice('user %user to the Sendgrid List', ['%user' => $email]); | |
return $response; | |
} | |
return $entity; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function executeMultiple(array $objects) | |
{ | |
$service = \Drupal::service('offering_sync_sendgrid.sync_sendgrid'); | |
$emails = []; | |
foreach ($objects as $entity) { | |
array_push($emails, $entity->getEmail()); | |
} | |
$response = $service->addContactsToSendgrid($emails); | |
if ($response) { | |
\Drupal::logger('offering_sync_sendgrid')->notice('users %users added to Sendgrid List', | |
['%users' => implode(",", $emails)]); | |
} | |
return $response; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) | |
{ | |
// If certain fields are updated, access should be checked against them as well. | |
// @see Drupal\Core\Field\FieldUpdateActionBase::access(). | |
return $object->access('update', $account, $return_as_object); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment