首页 > 解决方案 > 如何连接代码以更改 Drupal 模块行为?

问题描述

Drupal 不允许我删除扩展名(recurring_commerce),因为某些字段需要手动删除。这个问题在下面详细描述。

解决方案是向文件中添加一些代码,如下面问题的 #11 中所述。

基本上我需要以编程方式删除一个字段,而我以前从未这样做过。写一种钩子(?)。

我尝试在原始代码之后粘贴建议的代码,并用建议的代码替换原始代码。

每次更改代码后,我都会重新启动服务器,运行 cron 并清除缓存。

我仍然无法从 drupal 卸载 exension,因为我收到以下消息:

The following reason prevents Commerce Recurring from being uninstalled:

    The Billing period field type is used in the following fields: commerce_order.billing_period, commerce_order_item.billing_period

以下问题的详细信息:

请跳至#11

https://www.drupal.org/project/commerce_recurring/issues/2924965

建议应用代码:


function commerce_recurring_update_8101(&$sandbox) {

\Drupal::entityManager()->getStorage('field_config')->load('commerce_order.recurring.billing_period')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order.recurring.billing_schedule')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order.recurring.order_items')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order_item.recurring_standalone.billing_period')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order_item.recurring_standalone.subscription')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order_item.recurring_product_variation.billing_period')->delete();
\Drupal::entityManager()->getStorage('field_config')->load('commerce_order_item.recurring_product_variation.subscription')->delete();
}

function commerce_recurring_update_8102(&$sandbox) {
\Drupal::entityTypeManager()->getStorage('commerce_order_type')->load('recurring')->delete();
\Drupal::entityTypeManager()->getStorage('commerce_order_item_type')->load('recurring_standalone')->delete();
\Drupal::entityTypeManager()->getStorage('commerce_order_item_type')->load('recurring_product_variation')->delete();

}

我文件中的原始代码:

/**
 * Add the 'trial_starts' and "trial_ends" fields to subscriptions.
 */
function commerce_recurring_update_8101(&$sandbox) {
  $fields = [];
  $fields['trial_starts'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Trial starts'))
    ->setDescription(t('The time when the subscription trial starts.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'timestamp',
      'weight' => 0,
    ])
    ->setDisplayOptions('form', [
      'type' => 'datetime_timestamp',
      'weight' => 0,
    ])
    ->setDisplayConfigurable('form', TRUE);

  $fields['trial_ends'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Trial ends'))
    ->setDescription(t('The time when the subscription trial ends.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'timestamp',
      'weight' => 0,
    ])
    ->setDisplayOptions('form', [
      'type' => 'datetime_timestamp',
      'weight' => 0,
 ])
    ->setDisplayOptions('form', [
      'type' => 'datetime_timestamp',
      'weight' => 0,
    ])
    ->setDisplayConfigurable('form', TRUE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  foreach ($fields as $name => $storage_definition) {
    $update_manager->installFieldStorageDefinition($name, 'commerce_subscription', 'commerce_recurring', $storage_definition);
  }
}

/**
 * Make the billing_schedule field required on subscriptions.
 */
function commerce_recurring_update_8102() {
  $entity_definition_update = \Drupal::entityDefinitionUpdateManager();
  $field_definition = $entity_definition_update->getFieldStorageDefinition('billing_schedule', 'commerce_subscription');
  $field_definition->setRequired(TRUE);
  $entity_definition_update->updateFieldStorageDefinition($field_definition);
}

标签: drupaldrupal-8

解决方案


推荐阅读