Drupal 8 field_purge_batch错误

在使用了一段Drupal 8时间以后,一直出现一个错误.

Error: Call to a member function getTargetEntityTypeId() on array in field_purge_batch() (line 82 of /Users/xiukun/www/drupal/core/modules/field/field.purge.inc) #0 /Users/xiukun/www/drupal/core/modules/field/field.module(167): field_purge_batch(50) #1 [internal function]: field_cron() #2 /Users/xiukun/www/drupal/core/lib/Drupal/Core/Extension/ModuleHandler.php(391): call_user_func_array('field_cron', Array) #3 /Users/xiukun/www/drupal/core/lib/Drupal/Core/Cron.php(235): Drupal\Core\Extension\ModuleHandler->invoke('field', 'cron') #4 /Users/xiukun/www/drupal/core/lib/Drupal/Core/Cron.php(133): Drupal\Core\Cron->invokeCronHandlers() #5 /Users/xiukun/www/drupal/core/lib/Drupal/Core/ProxyClass/Cron.php(75): Drupal\Core\Cron->run() #6 /Users/xiukun/www/drupal/core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php(65): Drupal\Core\ProxyClass\Cron->run() #7 /Users/xiukun/www/drupal/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(108): Drupal\automated_cron\EventSubscriber\AutomatedCron->onTerminate(Object(Symfony\Component\HttpKernel\Event\PostResponseEvent), 'kernel.terminat...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #8 /Users/xiukun/www/drupal/vendor/symfony/http-kernel/HttpKernel.php(88): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.terminat...', Object(Symfony\Component\HttpKernel\Event\PostResponseEvent)) #9 /Users/xiukun/www/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(32): Symfony\Component\HttpKernel\HttpKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Cache\CacheableResponse)) #10 /Users/xiukun/www/drupal/core/lib/Drupal/Core/DrupalKernel.php(635): Stack\StackedHttpKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Cache\CacheableResponse)) #11 /Users/xiukun/www/drupal/index.php(22): Drupal\Core\DrupalKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Cache\CacheableResponse)) #12 {main}.

接着我去找到空上函数,打印了一下数据

$deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
$fields = $deleted_fields_repository->getFieldDefinitions(NULL);
print_r($fields);

发现第一个第二个是数组,数组怎么会有对象呢
但是这里也就是获取一个Type,所以我去修改代码.

foreach ($fields as $field) {
  if(!is_array($field)) {
     $entity_type = $field->getTargetEntityTypeId();
     .....

TO

foreach ($fields as $field) {
  if(!is_array($field)) {
     if(!is_array($field)) {
       $entity_type = $field->getTargetEntityTypeId();
     }else{
       $entity_type = $field['entity_type'];
     }
     .....

接着再Run : field_purge_batch in devel。 Fixed. 这个问题估计是删除字段的时候没有删除干净

Drupal 8 字段

获取字段配置:

$field = FieldStorageConfig::loadByName('node', 'field_module_version')
// 获取所有配置
print_r($field->getSettings());


// 获取字段允许字段
$field->getSetting('allowed_values');