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 entityQuery

# 获取所有的Node,返回ID
$ids = \Drupal::entityQuery('node')->execute();
dpm($ids);

# 根据条件进行查询
$EntityQueryGetId = \Drupal::entityQuery('project_list')
->condition('field_short_name', $content['field_module_name'])->execute();

	

Drupal 8 Entity常用

获取EntityManager
$entity_manager = \Drupal::entityTypeManager();

根据Property属性查询
$properties = array(
   'deleted' => TRUE,
   'include_deleted' => TRUE,
);
$fields = $entity_manager->getStorage('field_config')->loadByProperties($properties);

获取所有已定义的EntityType
$info = $entity_manager->getDefinitions();


获取一个Entity:

$entity = \Drupal::entityTypeManager()->getStorage('MyEntityType')->load(9);
dpm($entity);

查询其它条件

$entityQuery = \Drupal::entityQuery('project_list')
      ->condition('field_short_name', $short_name)->execute();

创建Entity

$EntityStorage = \Drupal::entityTypeManager()->getStorage('project_list');
$Entity = $EntityStorage->create(array('type' => 'project_list'));
$Entity->title = $title;
$Entity->field_short_name = $short_name;
$Entity->field_project_link = $link;

修改Entity 数据

$entity = \Drupal::entityTypeManager()->getStorage('MyEntityType')->load(9);
$entity->myfield = 1;
$entity->save();

Drupal 8定义一个block

http://drupal.org/node/1880620
blocks在drupal8已经变成了一个插件(plugin)形式.
Block types现在被定义为block plugins 而不是hook_block_info().。
blocks 现在支持多个block接口, 所以同一个block可以在主题多次使用. 每个block都有自己的配置并可以删除。
TODO:

API 的改变

hook_block_info()已经被删除 Block types 现在被新增加的 block plugin定义.
hook_block_info_alter(已经被删除。这个新的 hook_block_alter() 应该取而代之的被利用hook_block_configure(), hook_block_save(), and hook_block_view() 已经被 block plugin 的方法代替了.。
hook_block_list_alter()已经被删除, Modules 能够改变block 的可视性通过实施hook_block_access()来代替。
hook_block_MODULE_DELTA_alter()已经被 hook_block_ID_alter() an和hook_block_NAME_alter().取代。

案例

定义block 类型

将MyBlock.php plugin这个类文件放在下面这个目录中, Drupal 会自动判断它是一个block的插件:

my_module/lib/Drupal/my_module/Plugin/Block

(请看标准类加载),当启用my_module时, 这个插件类文件会被自动引用