You are here

Drupal8 实体开发常见错误

作者: 老葛 亚艾元软件

1,Drupal\Core\Extension\InfoParserExceptionDrupal\Core\Extension\InfoParserDynamic->parse() 中的 Missing required keys (core) in modules/anjian/anjian.info.yml (D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Extension\InfoParserDynamic.php 的第 29 )

 

这个是模块的.info.yml文件中缺少了:

core: 8.x

注意这个文件中,有几项是必填的。

 

2, 实体模块启用了,但是数据库表没有创建,导致无法卸载:

Drupal\Core\Database\DatabaseExceptionWrapperDrupal\Core\Entity\ContentEntityStorageBase->hasData() 中的 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shiyao.anjian' doesn't exist: SELECT base_table.id AS id, base_table.id AS base_table_id FROM {anjian} base_table LIMIT 1 OFFSET 0; Array ( ) (D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php 的第 72 )

 

解决办法:

通过phpmyadmin,手动创建一个同名数据库表,这样可以使得卸载模块成功。

 

3,引用的类、接口,不存在导致异常错误。

比如这行代码:

class Anjian extends ContentEntityBase implements AnjianInterface {

 

但是在上面的use语句里面,遗漏了:

use Drupal\anjian\AnjianInterface;

导致程序找不到这个接口。


错误4

Fatal error: Call to a member function getFieldStorageDefinition() on a non-object in D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Entity\ContentEntityBase.php on line 1184

原因:

Cases实体定义里面,有这么一句话:

*   admin_permission = "access administration pages",

 *   base_table = "cases",

 *   entity_keys = {

 *     "id" = "id",

 *     "label" = "name"

 *   },

 

错误原因,是这里的name,没有在Cases实体的baseFieldDefinitions里面定义。

$fields['id'] = BaseFieldDefinition::create('integer')

      ->setLabel(t('Case ID'))

      ->setDescription(t('The case integer ID.'))

      ->setReadOnly(TRUE)

      ->setSetting('unsigned', TRUE);

 

    $fields['code'] = BaseFieldDefinition::create('string')

      ->setLabel(t('编码'))

      ->setDescription(t('案件的编码.'))

      ->setSetting('max_length', 255)

      ->setDisplayOptions('form', array(

        'type' => 'string_textfield',

        'weight' => -5,

      ))

      ->setDisplayConfigurable('form', TRUE);

暂时先使用已经定义的code,作为label即可。

 

 

错误5

entity.cases.delete_form:

  path: '/case/{cases}/delete'

  defaults:

    _entity_form: 'cases.delete'

    _title: 'Delete case'

  requirements:

    _permission: 'delete case'

taxonomy_term: \d+

改为:

entity.cases.delete_form:

  path: '/case/{cases}/delete'

  defaults:

    _entity_form: 'cases.delete'

    _title: 'Delete case'

  requirements:

    _permission: 'delete case'

    cases: \d+

taxonomy term的地方拷贝的代码。没有修改完毕。

 

 

错误6

Symfony\Component\Routing\Exception\MissingMandatoryParametersExceptionDrupal\Core\Routing\UrlGenerator->doGenerate() 中的 Some mandatory parameters are missing ("cases") to generate a URL for route "entity.cases.canonical". (D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Routing\UrlGenerator.php 的第 180 )

 

检查,代码:

  public function getCancelUrl() {

    // The cancel URL is the vocabulary collection, terms have no global

    // list page.

    return new Url('entity.cases.canonical');

  }

应该改为:

  public function getCancelUrl() {

    // The cancel URL is the vocabulary collection, terms have no global

    // list page.

    return new Url('entity.cases.canonical', ['cases' => $this->entity->id()]);

  }

 

Url函数里面,有些需要传递参数过去。参数不能缺失。

 

 

错误7:

Symfony\Component\Routing\Exception\RouteNotFoundExceptionDrupal\Core\Routing\RouteProvider->getRouteByName() 中的 Route "entity.cases.canonical" does not exist. (D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Routing\RouteProvider.php 的第 190 )

 

解决办法:

在模块的.routing.yml文件中,增加路由的定义。

entity.cases.canonical:

  path: '/case/{cases}'

  defaults:

    _entity_view: 'cases.full'

    _title: '案件'

    _title_callback: '\Drupal\cases\Controller\CasesController::caseTitle'

  requirements:

    _entity_access: 'cases.view'

cases: \d+

 

错误8

网站遇到了不可预知的错误。请稍后再试。

使用phpmyadmin检查数据库,查看watchdog

Symfony\Component\Routing\Exception\RouteNotFoundException, Route "system.admin_config_case" does not exist. Function Drupal\Core\Routing\RouteProvider->getRouteByName() "D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Routing\RouteProvider.php D:\xampp\htdocs\shiyao\core\lib\Drupal\Core\Routing\UrlGenerator.php(419): Drupal\Core\Routing\RouteProvider->getRouteByName('system.admin_co...')

 

通过数据库清除所有缓存,打开menu_tree数据库表,找到system.admin_config_case所在的记录,手动删除这个记录。问题解决。

 

 

问题9

为配置页面(admin/config)增加一个区块,

Routings.yml文件,定义对应的路由:

cases.admin_config_case:

  path: '/admin/config/cases'

  defaults:

    _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'

    _title: 'Case'

  requirements:

    _permission: 'access administration pages'

cases.config_default:

  path: '/admin/config/case/default'

  defaults:

    _form: 'Drupal\cases\Form\CasesConfigForm'

    _title: 'Config case'

  requirements:

_permission: 'access administration pages'

 

Links.menu.yml文件中定义对应的菜单:

cases.admin_config_case:

  route_name: cases.admin_config_case

  parent: system.admin_config

  title: Case

  description: 'Configure case.'

  weight: -10

cases.config_default:

  title: Config case

  description: 'Adjust configuration settings for cases.'

  route_name: cases.config_default

  parent: cases.admin_config_case

  weight: 0

关键是控制器使用:

\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage



论坛:

Drupal版本: