You are here

commerce_invoice_pdf.rules.inc in Commerce Invoice 7.2

Rules integration for the Commerce Invoice PDF module.

File

modules/pdf/commerce_invoice_pdf.rules.inc
View source
<?php

/**
 * @file
 * Rules integration for the Commerce Invoice PDF module.
 */
use Drupal\commerce_invoice\Entity\Invoice;

/**
 * Implements hook_rules_event_info().
 */
function commerce_invoice_pdf_rules_event_info() {
  $events = array();
  $events['commerce_invoice_pdf_created'] = array(
    'label' => t('After creating an invoice PDF'),
    'group' => t('Commerce Invoice'),
    'variables' => array(
      'commerce_invoice' => array(
        'type' => 'commerce_invoice',
        'label' => t('The invoice entity'),
      ),
      'file' => array(
        'type' => 'file',
        'label' => t('The file entity of the newly created invoice PDF'),
      ),
    ),
  );
  return $events;
}

/**
 * Implements hook_rules_action_info().
 */
function commerce_invoice_pdf_rules_action_info() {
  $actions = array();
  $actions['commerce_invoice_pdf_create'] = array(
    'base' => 'commerce_invoice_pdf_action_create',
    'label' => t('Create a PDF'),
    'parameter' => [
      'invoice' => [
        'type' => 'commerce_invoice',
        'label' => t('Invoice'),
      ],
      'recreate' => [
        'type' => 'boolean',
        'label' => t('Recreate existing'),
        'description' => t('Recreate (replace) the file if it already exists.'),
        'default value' => 1,
      ],
    ],
    'group' => t('Commerce Invoice'),
  );
  if (module_exists('advancedqueue')) {
    $actions['commerce_invoice_pdf_create']['parameter']['queue'] = [
      'type' => 'boolean',
      'label' => t('Use Advanced Queue'),
      'description' => t('Do not create PDF immediately. Instead, add the task to Advanced Queue.'),
      'default value' => 1,
      'optional' => TRUE,
    ];
  }
  return $actions;
}

/**
 * Rules action: Create a PDF file for a given invoice entity.
 *
 * @param Invoice $invoice
 *   The invoice entity
 * @param boolean $recreate
 *   Whether to recreate (replace) the file if it already exists.
 * @param boolean $queue
 *   Whether to add the PDF creation into a queue or not.
 */
function commerce_invoice_pdf_action_create(Invoice $invoice, $recreate, $queue = FALSE) {
  if (!$queue) {
    _commerce_invoice_pdf_ensure_default_theme();
    commerce_invoice_pdf_create($invoice, TRUE, $recreate);
  }
  else {
    $queue = DrupalQueue::get('commerce_invoice_pdf_create_queue');
    $task = array(
      'invoice_id' => $invoice->invoice_id,
      'recreate' => $recreate,
    );
    $queue
      ->createItem($task);
  }
}

Functions

Namesort descending Description
commerce_invoice_pdf_action_create Rules action: Create a PDF file for a given invoice entity.
commerce_invoice_pdf_rules_action_info Implements hook_rules_action_info().
commerce_invoice_pdf_rules_event_info Implements hook_rules_event_info().