You are here

commerce_invoice.install in Commerce Invoice 8.2

Same filename and directory in other branches
  1. 7.2 commerce_invoice.install
  2. 7 commerce_invoice.install

Contains install and update functions for Commerce Invoice.

File

commerce_invoice.install
View source
<?php

/**
 * @file
 * Contains install and update functions for Commerce Invoice.
 */
use Drupal\commerce_invoice\Entity\Invoice;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StreamWrapper\PrivateStream;

/**
 * Implements hook_requirements().
 */
function commerce_invoice_requirements($phase) {
  $requirements = [];

  // Ensure the private file system path is configured.
  if (in_array($phase, [
    'install',
    'runtime',
  ]) && !PrivateStream::basePath()) {
    $requirements['commerce_invoice_private_path'] = [
      'title' => t('Private file system path'),
      'description' => t('Commerce Invoice requires the private file system path to be configured.'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  return $requirements;
}

/**
 * Mark the invoice owner field as non translatable.
 */
function commerce_invoice_update_8200() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $field_definition = $definition_update_manager
    ->getFieldStorageDefinition('uid', 'commerce_invoice');
  $field_definition
    ->setTranslatable(FALSE);
  $definition_update_manager
    ->updateFieldStorageDefinition($field_definition);
}

/**
 * Update the 'uid' field.
 */
function commerce_invoice_update_8201() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $base_field_override_storage = \Drupal::entityTypeManager()
    ->getStorage('base_field_override');
  $storage_definition = $definition_update_manager
    ->getFieldStorageDefinition('uid', 'commerce_invoice');
  $default_value_callback = Invoice::class . '::getCurrentUserId';
  $base_field_overrides = $base_field_override_storage
    ->loadByProperties([
    'entity_type' => 'commerce_invoice',
    'field_name' => 'uid',
  ]);

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $base_field_override */
  foreach ($base_field_overrides as $base_field_override) {
    if ($base_field_override
      ->getDefaultValueCallback() !== $storage_definition
      ->getDefaultValueCallback()) {
      continue;
    }

    // Update the "default_value_callback" for base field overrides, as long
    // as they're using the default one.
    $base_field_override
      ->setDefaultValueCallback($default_value_callback);
    $base_field_override
      ->save();
  }
  $storage_definition
    ->setDefaultValueCallback($default_value_callback);
  $definition_update_manager
    ->updateFieldStorageDefinition($storage_definition);
}

/**
 * Add a file field to invoices that holds a reference to the PDF file.
 */
function commerce_invoice_update_8202() {
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Invoice PDF'))
    ->setSetting('target_type', 'file')
    ->setDescription(t('The invoice PDF file.'))
    ->setDisplayConfigurable('view', TRUE);
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $definition_manager
    ->installFieldStorageDefinition('invoice_file', 'commerce_invoice', 'commerce_invoice', $storage_definition);
}

Functions

Namesort descending Description
commerce_invoice_requirements Implements hook_requirements().
commerce_invoice_update_8200 Mark the invoice owner field as non translatable.
commerce_invoice_update_8201 Update the 'uid' field.
commerce_invoice_update_8202 Add a file field to invoices that holds a reference to the PDF file.