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');

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 7制作可滚动的Banner

需要的模块:config_pages(用于配置Banner图片,需要一个多图字段), flexslider, linkimage

function mymodule_block_info() {
  // This example comes from node.module.
  $blocks['banner'] = array(
    'info' => t('Banner'),
    'cache' => DRUPAL_NO_CACHE
  );

  return $blocks;
}

function mymodule_block_view($delta = '') {
  if($delta == 'banner') {

    $config = config_pages_config('banner');
    if ($config) {
      // load flexslider.
      $optionset = flexslider_optionset_load('default');
      flexslider_add('block-invest-banner', $optionset);
    }

    // render item.
    $items = array();
    foreach($config->raw()->field_banner_slider['und'] as $image_item) {
      $items[] = theme('linkimage_formatter', array('item' => $image_item, 'image_style' => 'newbanner'));
    }
    $item_list = theme('item_list', array('items' => $items, 'attributes' => array('class' => array('slides'))));
    return array(
      'content' => '
' . $item_list . '
', ); } }

mysql 分区

分区的作用是将一个表或者索引物分解为更小,更可管理的部分, 如果表中存在主键或者是唯一索引时,分区列必须是唯一索引的一部分
比如:
查询数据系统只会获取指定的分区.
数据量会变小
易于管理

查看是否启用分区:

mysql> show plugins;
Name: partition

分区的类型:
range分区: 行数据基于属于一个给定连续区间的列值放入分区, 必须明确的指定一个给定的列值和列值集
list分区:和range分区类似, 只是list分区面向的是离散的值, 必须明确的指定一个给定的列值和列值集
hash分区: 根据用户自定义的表达式返回的值进行分区, 返回的值不能为负数, 目的是将数据均匀的分布在预先定义的分区中
key分区:根据mysql数据库提供的哈希函数来进行分区

查看分区信息: select * from information_schema.PARTITIONS where table_name='sales'
查看sql查询的分区信息:explain partitions select * from mysqltest.sales where date>='2016-01-01' and date<'2017-01-01';
删除分区: alter table sales drop partiion p2018;

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();