contacts_events-8.x-1.x-dev/modules/printing/contacts_events_printing.module

modules/printing/contacts_events_printing.module
<?php

/**
 * @file
 * Primary module hooks for Contacts Events Printing module.
 */

use Drupal\contacts_events\Entity\EventType;
use Drupal\contacts_events\Entity\Ticket;
use Drupal\contacts_events\Entity\TicketType;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity\BundleFieldDefinition;

/**
 * Implements hook_theme().
 */
function contacts_events_printing_theme() {
  return [
    'ticket_printing_content' => [
      'template' => 'ticket-printing-content',
      'variables' => [
        'ticket' => NULL,
        'show_badge' => FALSE,
        'show_address' => FALSE,
        'logo' => '',
        'badge_logo' => '',
        'badge_image' => '',
        'order_id' => '',
        'contact_id' => '',
        'ticket_id' => '',
        'team' => '',
        'address' => '',
        'name' => '',
        'first_name' => '',
        'event_name' => '',
        'event_date' => '',
        'booking_reference' => '',
        'ticket_class' => '',
        'barcode' => '',
        'letter_text' => '',
        'company_address' => '',
        'company_registration' => '',
        'segments' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_entity_type_build().
 */
function contacts_events_printing_entity_type_build(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['contacts_ticket']->setLinkTemplate('booking_ticket', '/booking/{commerce_order}/ticket/{contacts_ticket}');
  $entity_types['contacts_ticket']->setLinkTemplate('booking_ticket_print', '/booking/{commerce_order}/ticket/{contacts_ticket}/print');
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Add ticket printing specific page template alter suggestion.
 */
function contacts_events_printing_theme_registry_alter(&$theme_registry) {
  $theme_registry['page__booking__ticket__render'] = [
    'template' => 'page--booking--ticket--render',
    'path' => drupal_get_path('module', 'contacts_events_printing') . '/templates',
    'base hook' => 'page',
  ];
}

/**
 * Implements hook_entity_field_storage_info().
 */
function contacts_events_printing_entity_field_storage_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type->id() === 'contacts_ticket') {
    $fields['printed'] = BundleFieldDefinition::create('boolean')
      ->setName('printed')
      ->setTargetEntityTypeId('contacts_ticket')
      ->setLabel(new TranslatableMarkup('Ticket printed'));

    $fields['print_log'] = BundleFieldDefinition::create('ce_print_log')
      ->setName('print_log')
      ->setTargetEntityTypeId('contacts_ticket')
      ->setSetting('target_type', 'user')
      ->setLabel(new TranslatableMarkup('Print log'));
  }
  return $fields;
}

/**
 * Implements hook_entity_bundle_field_info().
 */
function contacts_events_printing_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  $fields = [];
  if ($entity_type->id() === 'contacts_ticket') {
    $fields['printed'] = BundleFieldDefinition::create('boolean')
      ->setName('printed')
      ->setTargetEntityTypeId('contacts_ticket')
      ->setLabel(new TranslatableMarkup('Ticket printed'))
      ->setDisplayOptions('form', ['region' => 'hidden'])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['print_log'] = BundleFieldDefinition::create('ce_print_log')
      ->setName('print_log')
      ->setTargetEntityTypeId('contacts_ticket')
      ->setLabel(new TranslatableMarkup('Print log'))
      ->setSetting('target_type', 'user')
      ->setDisplayOptions('form', ['region' => 'hidden'])
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);
  }
  return $fields;
}

/**
 * Implements hook_entity_extra_field_info().
 */
function contacts_events_printing_entity_extra_field_info() {
  $extra = [];

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
  $field_manager = \Drupal::service('entity_field.manager');
  foreach (TicketType::loadMultiple() as $bundle) {
    $fields = $field_manager->getFieldDefinitions('contacts_ticket', $bundle->id());
    $definition = $fields['print_log'];
    $extra['contacts_ticket'][$bundle->id()]['form']['print_log:view'] = [
      'label' => $definition->getLabel(),
      'description' => $definition->getDescription(),
      'visible' => FALSE,
    ];
  }

  foreach (EventType::loadMultiple() as $bundle) {
    $extra['contacts_event'][$bundle->id()]['form']['settings_ticket_printing'] = [
      'label' => new TranslatableMarkup('Enable Ticket Printing'),
      'visible' => FALSE,
    ];
  }

  return $extra;
}

/**
 * Implements hook_contacts_events_ticket_form_alter().
 */
function contacts_events_printing_contacts_events_ticket_form_alter(array &$form, FormStateInterface $form_state, Ticket $ticket, string $display_mode) {
  $display = EntityFormDisplay::collectRenderDisplay($ticket, $display_mode);
  if ($options = $display->getComponent('print_log:view')) {
    $form['print_log'] = $ticket->get('print_log')->view();
    $form['print_log']['#weight'] = $options['weight'];
  }
}

/**
 * Implements hook_contacts_event_form_alter().
 */
function contacts_events_printing_form_contacts_event_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\contacts_events\Entity\EventInterface $entity */
  $entity = $form_state->getFormObject()->getEntity();
  $form['settings_ticket_printing'] = [
    '#type' => 'checkbox',
    '#title' => new TranslatableMarkup('Enable Ticket Printing'),
    '#description' => new TranslatableMarkup('Booking managers and Ticket holders can download paid in full tickets.'),
    '#default_value' => $entity->getSetting('printing.enabled'),
    '#parents' => array_merge($form['#parents'] ?? [],
      ['settings', 'printing', 'enabled']),
  ];
}

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

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