Sending an email from Drupal

Submitted bybarnettech onWed, 03/17/2021 - 14:10
 * Implements hook_mail().
function app_voucher_mail($key, &$message, $params) {
  $function = 'app_voucher_mail__' . $key;
  if (function_exists($function)) {
    return $function($key, $message, $params);

   * @I Convert the enrollment email to use the new mail handler
   *    type     : improvement
   *    priority : normal
   *    labels   : refactoring
  if (!empty($params['from'])) {
    $message['from'] = $params['from'];
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['body'];


To Drupal config import a single file or a handful of files

Submitted bybarnettech onTue, 03/16/2021 - 10:01

Copy the config file(s) to a temp directory called only-few-configs (or whatver you like) and run drush cim --partial --source=/full/pathTo/only-few-configs/

This should have worked supposedly: drupal config:import:single --directory="/Users/jamesbarnett/sites/IPC/ipcedtr/config/default" --file="views.view.confirm_message_product_display.yml". (might need drupal console?)

Two different ways to query entities / nodes: entityQuery and entityTypeManager

Submitted bybarnettech onTue, 03/09/2021 - 15:32
 $results = \Drupal::entityQuery('app_voucher')
      ->range(0, 20000)
foreach ($results as $result) {
      $messenger = \Drupal::messenger();
      $voucher_entity = \Drupal::entityTypeManager()->getStorage("app_voucher")->load($result);
      $voucher_id = $voucher_entity->voucher_id->getValue();
      $product_id = $voucher_entity->product_id->getValue();


Here are 2 different ways to get the same sort of data:

code to grab a field value from a D8 group module member field

Submitted bybarnettech onFri, 01/29/2021 - 10:23

code to grab a field value from a group module member field

  $group =  \Drupal\group\Entity\Group::load(31);
  $group_memberships = \Drupal::service('group.membership_loader')->loadByGroup($group);
  // $results = $query->execute();
  foreach ($group_memberships as $member) {
    //$group_id[] = $member->groupContent->values["field_group_id"]["x-default"][0]["value"];
    $group_id[] = $member->getGroupContent()->get("field_group_id")->value;
  $options = array_filter($group_id);

This worked to grab the uid from the member

Add JavaScript and external JavaScript in Service Now

Submitted bybarnettech onWed, 12/09/2020 - 14:20

Adding a UI script allows for adding any sort of JavaScript, and it can be made global. In the left admin serach box, just type in "UI Script" and then go from there to add a new one, or search existing ones.

this worked for instance

var script = document.createElement('script'); 
script.src = ""; 

/*var script2 = document.createElement('script'); 
script2.src = "";