Search

Displaying 111 - 118 of 118

How to generate masquerade link programmatically

Components

The Masquerade module allows website admins to switch users and visit the website as that user. This is helpful for developers and site builders when trying to find out what a client might see when logged into the website. To create the unmasquerade programmatically use this:

if (
  \Drupal::moduleHandler()->moduleExists('masquerade') && 
  \Drupal::service('masquerade')->isMasquerading()
){
  $links['unmasquerade'] = [
    '#type' => 'link',
    '#title' => t('Unmasquerade'),
    '#url' => Url::fromRoute('masquerade.unmasquerade'),
    '#weight' => 1500,
  ];
}

Migrate existing Entity Reference field to Entity Reference Revisions field

Entity Reference and Entity Reference Revisions fields are very similar, but the latter offers more flexibility when it comes to reverting to a previous revision in the case when you have a nested entity. Let's see the easiest way to migrate an existing Entity Reference field to an Entity Reference Revisions field.

How to customize the Drupal Commerce order receipt email subject

Components

At the moment you can't configure the order receipt email subject. The subject is hardcoded in the OrderReceiptMail service.

There is an issue to fix this, but until that happens the only way to alter the email subject is to use the hook_mail_alter().

/**
 * Implements hook_mail_alter().
 */
function MY_MODULE_mail_alter(&$message) {
  if ($message['key'] == 'order_receipt' && !empty($message['params']['order'])) {
    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $message['params']['order'];

    $message['from'] = 'noreply@example.com';
    $message['subject'] = t('Receipt for Order ID: @number', [
      '@number' => $order->getOrderNumber(),
    ]);
  }
}