commerce_shipping-8.x-2.0-rc2/commerce_shipping.install

commerce_shipping.install
<?php

/**
 * @file
 * Contains install and update functions for Shipping.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\commerce_shipping\Event\ShipmentEvent;

/**
 * Remove the 'adjustments' field from shipments.
 */
function commerce_shipping_update_8200() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();

  $storage_definition = BaseFieldDefinition::create('commerce_adjustment')
    ->setLabel(t('Adjustments'))
    ->setTargetEntityTypeId('commerce_shipment')
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE);
  $entity_definition_update->uninstallFieldStorageDefinition($storage_definition);
}

/**
 * Add the condition fields to shipping methods.
 */
function commerce_shipping_update_8201() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();

  $storage_definition = BaseFieldDefinition::create('commerce_plugin_item:commerce_condition')
    ->setLabel(t('Conditions'))
    ->setTargetEntityTypeId('commerce_shipping_method')
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setDisplayOptions('form', [
      'type' => 'commerce_conditions',
      'weight' => 3,
      'settings' => [
        'entity_types' => ['commerce_order', 'commerce_shipment'],
      ],
    ]);
  $entity_definition_update->installFieldStorageDefinition('conditions', 'commerce_shipping_method', 'commerce_shipping', $storage_definition);

  $storage_definition = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Condition operator'))
    ->setDescription(t('The condition operator.'))
    ->setRequired(TRUE)
    ->setSetting('allowed_values', [
      'AND' => t('All conditions must pass'),
      'OR' => t('Only one condition must pass'),
    ])
    ->setDisplayOptions('form', [
      'type' => 'options_buttons',
      'weight' => 4,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDefaultValue('AND');
  $entity_definition_update->installFieldStorageDefinition('condition_operator', 'commerce_shipping_method', 'commerce_shipping', $storage_definition);
}

/**
 * Add the 'adjustments' field to shipments.
 */
function commerce_shipping_update_8202() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();

  $storage_definition = BaseFieldDefinition::create('commerce_adjustment')
    ->setLabel(t('Adjustments'))
    ->setTargetEntityTypeId('commerce_shipment')
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $entity_definition_update->installFieldStorageDefinition('adjustments', 'commerce_shipment', 'commerce_shipping', $storage_definition);
}

/**
 * Add the 'original_amount' field to shipments.
 */
function commerce_shipping_update_8203() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();

  $storage_definition = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Original amount'))
    ->setDescription(t('The original amount.'))
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE)
    ->setInitialValueFromField('amount');
  $entity_definition_update->installFieldStorageDefinition('original_amount', 'commerce_shipment', 'commerce_shipping', $storage_definition);
}

/**
 * Add created and changed fields to shipping methods.
 */
function commerce_shipping_update_8204() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $storage_definitions['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the shipping method was created.'))
    ->setTranslatable(TRUE)
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);

  $storage_definitions['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the shipping method was last edited.'))
    ->setTranslatable(TRUE);

  foreach ($storage_definitions as $name => $definition) {
    $definition_update_manager->installFieldStorageDefinition($name, 'commerce_shipping_method', 'commerce_shipping', $definition);
  }
}

/**
 * Add the event handler to the shipment entity type.
 */
function commerce_shipping_update_8205() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $entity_definition_update->getEntityType('commerce_shipment');
  $entity_type->setHandlerClass('event', ShipmentEvent::class);
  $entity_definition_update->updateEntityType($entity_type);
}

/**
 * Mark the shipping method "stores" field as optional.
 */
function commerce_shipping_update_8206() {
  \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
}

/**
 * Uninstall the created / changed fields added by content_translation.
 */
function commerce_shipping_update_8207() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  foreach (['content_translation_created', 'content_translation_changed'] as $field_name) {
    if ($field_storage_definition = $definition_update_manager->getFieldStorageDefinition($field_name, 'commerce_shipping_method')) {
      $definition_update_manager->uninstallFieldStorageDefinition($field_storage_definition);
    }
  }
}

/**
 * Add the "shipments" field in the order view display.
 */
function commerce_shipping_update_10301(): void {
  $entity_type_manager = \Drupal::entityTypeManager();

  // Update the "default" order display add "shipment" component.
  $view_display_storage = $entity_type_manager->getStorage('entity_view_display');
  $order_types_storage = $entity_type_manager->getStorage('commerce_order_type');
  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  foreach ($order_types_storage->loadMultiple() as $order_type) {
    $view_display_id = sprintf('commerce_order.%s.default', $order_type->id());
    $view_display = $view_display_storage->load($view_display_id);
    if (!($view_display instanceof EntityViewDisplayInterface)) {
      continue;
    }

    $view_display->setComponent('shipments', [
      'type' => 'commerce_shipping_information',
    ]);
    $view_display->save();
  }
}

/**
 * Create a new view mode and displays for shipment.
 */
function commerce_shipping_update_10302(): void {
  $entity_type_manager = \Drupal::entityTypeManager();

  // Create a new view mode for shipment.
  $view_mode_storage = $entity_type_manager->getStorage('entity_view_mode');
  $view_mode = $view_mode_storage->load('commerce_shipment.admin');
  if (!$view_mode) {
    /** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */
    $view_mode = $view_mode_storage->create([
      'id' => 'commerce_shipment.admin',
      'label' => 'Admin',
      'targetEntityType' => 'commerce_shipment',
    ]);
    $view_mode->save();
  }

  // Create entity view displays for the "Admin" view mode.
  $view_display_components = [
    'shipping_profile' => [
      'type' => 'entity_reference_revisions_entity_view',
      'label' => 'hidden',
      'weight' => 0,
      'settings' => [
        'view_mode' => 'admin',
      ],
    ],
    'shipping_method' => [
      'type' => 'commerce_shipping_method',
      'label' => 'inline',
      'weight' => 1,
    ],
    'amount' => [
      'type' => 'commerce_price_default',
      'label' => 'inline',
      'weight' => 2,
    ],
    'tracking_code' => [
      'type' => 'commerce_tracking_link',
      'label' => 'inline',
      'weight' => 3,
    ],
  ];
  $view_display_storage = $entity_type_manager->getStorage('entity_view_display');
  $shipment_types_storage = $entity_type_manager->getStorage('commerce_shipment_type');
  /** @var \Drupal\commerce_shipping\Entity\ShipmentTypeInterface[] $shipment_types */
  $shipment_types = $shipment_types_storage->loadMultiple();
  foreach ($shipment_types as $shipment_type) {
    $view_display_id = sprintf('commerce_shipment.%s.admin', $shipment_type->id());
    $view_display = $view_display_storage->load($view_display_id);
    if (!($view_display instanceof EntityViewDisplayInterface)) {
      /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
      $view_display = $view_display_storage->create([
        'id' => $view_display_id,
        'targetEntityType' => 'commerce_shipment',
        'bundle' => $shipment_type->id(),
        'mode' => 'admin',
      ]);
      $view_display->enable();
      $view_display->save();
    }

    // After saving view display we may have more components than it should.
    // We need to rearrange them and add to the "hidden" list.
    $hidden_components = $view_display->get('hidden');
    foreach (array_keys($view_display->getComponents()) as $component_name) {
      if (!in_array($component_name, array_keys($view_display_components))) {
        $view_display->removeComponent($component_name);
        $hidden_components[$component_name] = TRUE;
      }
    }

    // Set required components in the view display.
    foreach ($view_display_components as $component_name => $component) {
      $view_display->setComponent($component_name, $component);
    }

    ksort($hidden_components);
    $view_display->set('hidden', $hidden_components);
    $view_display->save();
  }
}

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

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