qr_generator-1.0.1/qr_generator.module

qr_generator.module
<?php

/**
 * @file
 * Primary module hooks for QR-Generator module.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;

/**
 * Implements hook_theme().
 */
function qr_generator_theme(): array
{
  return [
    'qr_code' => ['render element' => 'elements'],
  ];
}

/**
 * Prepares variables for qr code templates.
 *
 * Default template: qr-code.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the qr code information and any
 *     fields attached to the entity.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_qr_code(array &$variables): void
{
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}

/**
 * Implements hook_user_cancel().
 */
function qr_generator_user_cancel($edit, UserInterface $account, $method): void
{
  switch ($method) {
    case 'user_cancel_block_unpublish':
      // Unpublish qr codes.
      $storage = \Drupal::entityTypeManager()->getStorage('qr_code');
      $qr_code_ids = $storage->getQuery()
        ->condition('uid', $account->id())
        ->condition('status', 1)
        ->accessCheck(FALSE)
        ->execute();
      foreach ($storage->loadMultiple($qr_code_ids) as $qr_code) {
        $qr_code->set('status', FALSE)->save();
      }
      break;

    case 'user_cancel_reassign':
      // Anonymize qr codes.
      $storage = \Drupal::entityTypeManager()->getStorage('qr_code');
      $qr_code_ids = $storage->getQuery()
        ->condition('uid', $account->id())
        ->accessCheck(FALSE)
        ->execute();
      foreach ($storage->loadMultiple($qr_code_ids) as $qr_code) {
        $qr_code->setOwnerId(0)->save();
      }
      break;
  }
}

/**
 * Implements hook_ENTITY_TYPE_predelete() for user entities.
 */
function qr_generator_user_predelete(UserInterface $account): void
{
  // Delete qr codes that belong to this account.
  $storage = \Drupal::entityTypeManager()->getStorage('qr_code');
  $qr_code_ids = $storage->getQuery()
    ->condition('uid', $account->id())
    ->accessCheck(FALSE)
    ->execute();
  $storage->delete(
    $storage->loadMultiple($qr_code_ids)
  );
}


/**
 * Implements hook_entity_operation_alter().
 */
function qr_generator_entity_operation_alter(array &$operations, EntityInterface $entity)
{
  if ($entity->getEntityTypeId() === 'qr_code') {
    $operations['qr_code'] = [
      'title' => t('Export'),
      'weight' => -1,
      'url' => \Drupal\Core\Url::fromRoute('qr_generator.export.form', ['qr_code' => $entity->id()]),
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc