【问题标题】:Entity.entity_name.collection link missing when creating a custom entity type创建自定义实体类型时缺少 Entity.entity_name.collection 链接
【发布时间】:2018-09-27 12:58:44
【问题描述】:

我一直在尝试以编程方式创建自定义实体类型,但我不断收到一条我似乎无法弄清楚的错误消息。这是我的代码:

/src/Entity/Car.php

namespace Drupal\car\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;

/**
 * Defines the Car entity.
 *
 * @ContentEntityType(
 *   id = "car",
 *   label = @Translation("Car"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\car\CarListBuilder",
*
 *     "form" = {
 *       "default" = "Drupal\car\Form\CarForm",
 *       "add" = "Drupal\car\Form\CarForm",
 *       "edit" = "Drupal\car\Form\CarForm",
 *       "delete" = "Drupal\car\Form\ContentEntityDeleteForm",
 *     },
 *     "route_provider" = {
  *      "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider"
 *     },
 *   },
 *   base_table = "car",
 *   admin_permission = "administer site entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/car/{car}",
 *     "add-form" = "/admin/structure/car/add",
 *     "edit-form" = "/admin/structure/car/{car}/edit",
 *     "delete-form" = "/admin/structure/car/{car}/delete",
 *     "collection" = "/admin/structure/car",
 *   },
 * )
 */

class Car extends ContentEntityBase implements CarInterface {

  use EntityChangedTrait;

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->get('name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the car entity.'))
      ->setSettings([
        'max_length' => 50,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the entity was created.'));

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the entity was last edited.'));

    return $fields;
  }

}

/src/Entity/CarInterface.php

namespace Drupal\car\Entity;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;

/**
 * Provides an interface for defining Car entities.
 */
interface CarInterface extends ContentEntityInterface, EntityChangedInterface {

  /**
   * Gets the Car name.
   *
   * @return string
   *   Name of the Car.
   */
  public function getName();

  /**
   * Sets the Car name.
   *
   * @param string $name
   *   The Car name.
   *
   * @return \Drupal\car\Entity\CarInterface
   *   The called Car entity.
   */
  public function setName($name);

}

/car.links.menu.yml

# Car menu items definition
entity.car.collection:
  title: 'Car list'
  route_name: entity.car.collection
  description: 'List Car entities'
  parent: system.admin_structure
  weight: 100

我也有 src/CarListBuilder.php(我认为内容与问题无关)

我得到的错误是:

路线“entity.car.collection”不存在。在...

我检查了路由器表,看起来五个链接中只有四个(“canonical”、“add-form”、“edit-form”、“delete-form”)被创建,但不是“collection” .

我检查了由 Drupal 控制台的 generate:entity:content 生成的代码,但我在任何代码中都找不到除注释之外定义集合链接的其他位置。

另外,我注意到的另一件事是没有“汽车”base_table。

我在这里错过了什么?

【问题讨论】:

    标签: php drupal drupal-8


    【解决方案1】:

    我刚刚使用 Drupal 8.6 进行了测试,对我来说效果很好,这些是我经历的步骤:

    第一步。$ drupal generate:entity:content

    // Welcome to the Drupal Content Entity generator
    Enter the module name [aa_11]:
    > mymodule
    
    Enter the class of your new content entity [DefaultEntity]:
    > Car
    
    Enter the machine name of your new content entity [car]:
    >
    
    Enter the label of your new content entity [Car]:
    >
    
    Enter the base-path for the content entity routes [/admin/structure]:
    >
    
    Do you want this (content) entity to have bundles? (yes/no) [no]:
    > no
    
    Is your entity translatable? (yes/no) [yes]:
    > no
    
    Is your entity revisionable? (yes/no) [yes]:
    > no
    

    第二步。$ drupal entity-updates -y

    结果文件:src/CarListBuilder.php

    namespace Drupal\mymodule;
    
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\Core\Entity\EntityListBuilder;
    use Drupal\Core\Link;
    
    /**
     * Defines a class to build a listing of Car entities.
     *
     * @ingroup mymodule
     */
    class CarListBuilder extends EntityListBuilder {
    
    
      /**
       * {@inheritdoc}
       */
      public function buildHeader() {
        $header['id'] = $this->t('Car ID');
        $header['name'] = $this->t('Name');
        return $header + parent::buildHeader();
      }
    
      /**
       * {@inheritdoc}
       */
      public function buildRow(EntityInterface $entity) {
        /* @var $entity \Drupal\mymodule\Entity\Car */
        $row['id'] = $entity->id();
        $row['name'] = Link::createFromRoute(
          $entity->label(),
          'entity.car.edit_form',
          ['car' => $entity->id()]
        );
        return $row + parent::buildRow($entity);
      }
    
    }
    

    【讨论】:

    • Cesar:使用 Drupal 控制台生成实体也适用于我。我正在尝试学习 Drupal 8 开发,并想了解每一段代码的作用。我粘贴的代码是我在阅读了一些资源后自己编写的。如果您尝试使用我的代码,您会看到由于某种原因,“路由器”表中没有创建“收藏”链接。我试图找出原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多