你在这里

在 Drupal 8 中创建内容型实体类型 (翻译中)

原文地址: https://www.drupal.org/node/2192175

原文版本:  September 23, 2014.

 

This page provides an example of how to create a content entity type, with administration management pages, for Drupal 8.

Note: We try to improve the module as well as the documentation in sync with the development process of core. It is intended for usage with the latest dev version. You can also have a peek in core modules such as node and comment which make use of the ContentEntityType.

If you run into troubles while creating your own Content Entity be sure to check your watchdog log. Additionally inspect your apache log too. It will inform you about common mistakes such as missing use-statements for which Drupal 8 will only response with a white screen of death/WSOD (HTTP 500) in this case.

Set up module and admin menu entry

foo_bar/foo_bar.info.yml

name: Foo Bar<br>type: module<br>description: 'Provides FooBar entity.'<br>package: FooBar<br>core: 8.x<br>dependencies:<br>&nbsp; - entity


foo_bar/foo_bar.module

The hook_menu_link_defaults() function defines an administrative menu entry for managing your foo_bar entity.

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Contains Drupal\foo_bar\foo_bar.module<br>&nbsp;*/<br>/**<br>&nbsp;* Implements hook_permission().<br>&nbsp;*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">foo_bar_permission</span><span style="color: #007700">() {<br>&nbsp; return array(<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'delete_foo_bar' </span><span style="color: #007700">=&gt; array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Delete entity content.'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp; ),<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'add_foo_bar' </span><span style="color: #007700">=&gt; array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Add entity content'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp; ),<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'view_foo_bar' </span><span style="color: #007700">=&gt; array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'View entity content'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp; ),<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'edit_foo_bar' </span><span style="color: #007700">=&gt; array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Edit entity content'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp; ),<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'admin_foo_bar' </span><span style="color: #007700">=&gt; array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Administer settings'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp; ),<br>&nbsp; );<br>}<br></span><span style="color: #FF8000">/**<br>&nbsp;* Implements hook_theme().<br>&nbsp;*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">bookmark_theme</span><span style="color: #007700">() {<br>&nbsp; </span><span style="color: #FF8000">/*<br>&nbsp;&nbsp;&nbsp; * You can implement this hook if you don't want an annoying watchdog message in your log.<br>&nbsp;&nbsp;&nbsp; * However, it isn't required to implement it.<br>&nbsp;&nbsp; */<br></span><span style="color: #007700">}<br></span><span style="color: #0000BB">?&gt;</span></span>


Routing

foo_bar/foo_bar.routing.yml

The routing.yml file defines the routes for the management pages: view, list, add, edit, delete and settings.

foo_bar.view:<br>&nbsp; path: '/foo-bar/{foo_bar}'<br>&nbsp; defaults:<br>&nbsp;&nbsp;&nbsp; _entity_view: 'foo_bar'<br>&nbsp;&nbsp;&nbsp; _title: 'Foo Bar Content'<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'view_foo_bar'<br>foo_bar.list:<br>&nbsp; path: '/foo-bar/list'<br>&nbsp; defaults:<br>&nbsp;&nbsp;&nbsp; _entity_list: 'foo_bar'<br>&nbsp;&nbsp;&nbsp; _title: 'Foo Bar Content List'<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'view_foo_bar'<br>foo_bar.add:<br>&nbsp; path: '/foo-bar/add'<br>&nbsp; defaults:<br>&nbsp;&nbsp;&nbsp; _entity_form: foo_bar.add<br>&nbsp;&nbsp;&nbsp; _title: 'Add foo bar Content'<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'add_foo_bar'<br>foo_bar.edit:<br>&nbsp; path: '/foo-bar/{foo_bar}/edit'<br>&nbsp; defaults:<br>&nbsp;&nbsp;&nbsp; _entity_form: foo_bar.edit<br>&nbsp;&nbsp;&nbsp; _title: 'Edit foo bar content'<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'edit_foo_bar'<br>foo_bar.delete:<br>&nbsp; path: '/foo-bar/{foo_bar}/delete'<br>&nbsp; defaults:<br>&nbsp;&nbsp;&nbsp; _entity_form: foo_bar.delete<br>&nbsp;&nbsp;&nbsp; _title: 'Delete foo bar Content '<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'delete_foo_bar'<br>foo_bar.settings:<br>&nbsp; path: 'admin/structure/foo_bar_settings'<br>&nbsp; defaults:<br>&nbsp;&nbsp; _form: '\Drupal\foo_bar\Entity\Form\FooBarSettingsForm'<br>&nbsp;&nbsp; _title: 'FooBar Settings'<br>&nbsp; requirements:<br>&nbsp;&nbsp;&nbsp; _permission: 'admin_foo_bar'

foo_bar/foo_bar.links.action.yml

This makes the "Add" link appear on the List page.

foo_bar.add:<br>&nbsp; route_name: foo_bar.add<br>&nbsp; title: 'Add Foo Bar Content'<br>&nbsp; appears_on:<br>&nbsp;&nbsp;&nbsp; - foo_bar.list


foo_bar/foo_bar.links.task.yml

The "View/Edit/Delete" tabs will appear on the entity view page.
The "Settings" tab will appear on the entity settings page.

foo_bar.settings_tab:<br>&nbsp; route_name: foo_bar.settings<br>&nbsp; title: 'Settings'<br>&nbsp; base_route: foo_bar.settings<br>foo_bar.view:<br>&nbsp; route_name: foo_bar.view<br>&nbsp; base_route: foo_bar.view<br>&nbsp; title: 'View'<br>foo_bar.page_edit:<br>&nbsp; route_name: foo_bar.edit<br>&nbsp; base_route: foo_bar.view<br>&nbsp; title: Edit<br>foo_bar.delete_confirm:<br>&nbsp; route_name: foo_bar.delete<br>&nbsp; base_route: foo_bar.view<br>&nbsp; title: Delete<br>&nbsp; weight: 10


Entity type classes

foo_bar/src/FooBarInterface.php

All additional fields, which are not entity_keys must be defined with a getter and a setter in your content entity interface.

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Contains \Drupal\foo_bar\FooBarInterface.<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityInterface</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityTypeInterface</span><span style="color: #007700">;<br></span><span style="color: #FF8000">/**<br>&nbsp;* Provides an interface defining a Foo Bar entity.<br>&nbsp;*/<br></span><span style="color: #007700">interface </span><span style="color: #0000BB">FooBarInterface </span><span style="color: #007700">extends </span><span style="color: #0000BB">EntityInterface </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Returns the identifier.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @return int<br>&nbsp;&nbsp; *&nbsp;&nbsp; The entity identifier.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">id</span><span style="color: #007700">();<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Returns the entity UUID (Universally Unique Identifier).<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * The UUID is guaranteed to be unique and can be used to identify an entity<br>&nbsp;&nbsp; * across multiple systems.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @return string<br>&nbsp;&nbsp; *&nbsp;&nbsp; The UUID of the entity.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">uuid</span><span style="color: #007700">();<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Return the Value of Foo Bar Field.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @return string<br>&nbsp;&nbsp; *&nbsp;&nbsp; The content of the field.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getFooBarField</span><span style="color: #007700">();<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Defines the base fields of the entity type.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @param string $entity_type<br>&nbsp;&nbsp; *&nbsp;&nbsp; Name of the entity type<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @return \Drupal\Core\Field\FieldDefinitionInterface[]<br>&nbsp;&nbsp; *&nbsp;&nbsp; An array of entity field definitions, keyed by field name.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public static function </span><span style="color: #0000BB">baseFieldDefinitions</span><span style="color: #007700">(</span><span style="color: #0000BB">EntityTypeInterface $entity_type</span><span style="color: #007700">);<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>


foo_bar/src/Entity/FooBar.php

This file defines the content entity class.

If you want to use the same form for "add" and "edit" then simply use "default" for both in the @ContentEntityType Annotation.

Note: hook_schema() was deprecated. It is only required to implement baseFieldDefinitions() for your database schema.

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Contains \Drupal\foo_bar\Entity\FooBar.<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityStorageInterface</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Field</span><span style="color: #007700">\</span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">ContentEntityBase</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">FooBarInterface</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityTypeInterface</span><span style="color: #007700">;<br></span><span style="color: #FF8000">/**<br>&nbsp;* Defines the Foo Bar entity.<br>&nbsp;*<br>&nbsp;* @ContentEntityType(<br>&nbsp;*&nbsp;&nbsp; id = "foo_bar",<br>&nbsp;*&nbsp;&nbsp; label = @Translation("FooBar entity"),<br>&nbsp;*&nbsp;&nbsp; handlers = {<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "list_builder" = "Drupal\foo_bar\Entity\Controller\FooBarListBuilder",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "form" = {<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "add" = "Drupal\foo_bar\Entity\Form\FooBarFormController",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "edit" = "Drupal\foo_bar\Entity\Form\FooBarFormController",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "delete" = "Drupal\foo_bar\Entity\Form\FooBarDeleteForm"<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; },<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "translation" = "Drupal\content_translation\ContentTranslationController"<br>&nbsp;*&nbsp;&nbsp; },<br>&nbsp;*&nbsp;&nbsp; base_table = "foo_bar",<br>&nbsp;*&nbsp;&nbsp; admin_permission = "admin_foo_bar",<br>&nbsp;*&nbsp;&nbsp; fieldable = TRUE,<br>&nbsp;*&nbsp;&nbsp; translatable = TRUE,<br>&nbsp;*&nbsp;&nbsp; entity_keys = {<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "id" = "fbid",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "label" = "name",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "uuid" = "uuid"<br>&nbsp;*&nbsp;&nbsp; },<br>&nbsp;*&nbsp;&nbsp; links = {<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "edit-form" = "foo_bar.edit",<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; "delete-form" = "foo_bar.delete"<br>&nbsp;*&nbsp;&nbsp; },<br>&nbsp;*&nbsp;&nbsp; field_ui_base_route = "foo_bar.settings",<br>&nbsp;* )<br>&nbsp;*/<br></span><span style="color: #007700">class </span><span style="color: #0000BB">FooBar </span><span style="color: #007700">extends </span><span style="color: #0000BB">ContentEntityBase </span><span style="color: #007700">implements </span><span style="color: #0000BB">FooBarInterface </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">id</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'fbid'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getFooBarField</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo_bar_field</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public static function </span><span style="color: #0000BB">preCreate</span><span style="color: #007700">(</span><span style="color: #0000BB">EntityStorageInterface $storage_controller</span><span style="color: #007700">, array &amp;</span><span style="color: #0000BB">$values</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">preCreate</span><span style="color: #007700">(</span><span style="color: #0000BB">$storage_controller</span><span style="color: #007700">, </span><span style="color: #0000BB">$values</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$values </span><span style="color: #007700">+= array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'user_id' </span><span style="color: #007700">=&gt; \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">currentUser</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">(),<br>&nbsp;&nbsp;&nbsp; );<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public static function </span><span style="color: #0000BB">baseFieldDefinitions</span><span style="color: #007700">(</span><span style="color: #0000BB">EntityTypeInterface $entity_type</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'fbid'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'integer'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'ID'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The ID of the FooBar entity.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setReadOnly</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'uuid'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'uuid'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'UUID'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The UUID of the FooBar entity.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setReadOnly</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'langcode'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'language'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Language code'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The language code of theFooBar entity.'</span><span style="color: #007700">));<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'string'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Name'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The name of the FooBar entity.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setTranslatable</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setPropertyConstraints</span><span style="color: #007700">(</span><span style="color: #DD0000">'value'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'Length' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'max' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">32</span><span style="color: #007700">)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setSettings</span><span style="color: #007700">(array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">''</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'max_length' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">255</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'text_processing' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayOptions</span><span style="color: #007700">(</span><span style="color: #DD0000">'view'</span><span style="color: #007700">, array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'above'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'string'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">6</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayOptions</span><span style="color: #007700">(</span><span style="color: #DD0000">'form'</span><span style="color: #007700">, array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'string'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">6</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayConfigurable</span><span style="color: #007700">(</span><span style="color: #DD0000">'form'</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayConfigurable</span><span style="color: #007700">(</span><span style="color: #DD0000">'view'</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'string'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Type'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The bundle of the FooBar entity.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_id'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'entity_reference'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'User ID'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The ID of the associated user.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setSettings</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'target_type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'user'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setTranslatable</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo_bar_field'</span><span style="color: #007700">] = </span><span style="color: #0000BB">FieldDefinition</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">'string'</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'First FooBar Field'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDescription</span><span style="color: #007700">(</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'One field of the FooBar entity.'</span><span style="color: #007700">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setTranslatable</span><span style="color: #007700">(</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setPropertyConstraints</span><span style="color: #007700">(</span><span style="color: #DD0000">'value'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'Length' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'max' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">32</span><span style="color: #007700">)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setSettings</span><span style="color: #007700">(array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">''</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'max_length' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">255</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'text_processing' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayOptions</span><span style="color: #007700">(</span><span style="color: #DD0000">'view'</span><span style="color: #007700">, array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'above'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'string'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayOptions</span><span style="color: #007700">(</span><span style="color: #DD0000">'form'</span><span style="color: #007700">, array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'string'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayConfigurable</span><span style="color: #007700">(</span><span style="color: #DD0000">'form'</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;</span><span style="color: #0000BB">setDisplayConfigurable</span><span style="color: #007700">(</span><span style="color: #DD0000">'view'</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$fields</span><span style="color: #007700">;<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>


Controllers

foo_bar/src/Entity/Form/FooBarFormController.php

Define the form for adding and editing FooBar entity content.
It is called by the '_entity_form' definition in the routing.

Note: You only need to add elements to the $fields[] which are not defined as setRequired(TRUE) and are without setDisplayOptions('form', array()) in your baseFieldDefinitions() .

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Definition of Drupal\foo_bar\Entity\Form\FooBarFormController.<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">ContentEntityForm</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Language</span><span style="color: #007700">\</span><span style="color: #0000BB">Language</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">\</span><span style="color: #0000BB">FormStateInterface</span><span style="color: #007700">;<br></span><span style="color: #FF8000">/**<br>&nbsp;* Form controller for the foo_bar entity edit forms.<br>&nbsp;*/<br></span><span style="color: #007700">class </span><span style="color: #0000BB">FooBarFormController </span><span style="color: #007700">extends </span><span style="color: #0000BB">ContentEntityForm </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Overrides Drupal\Core\Entity\EntityFormController::form().<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">form</span><span style="color: #007700">(array </span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #FF8000">/* @var $entity \Drupal\foo_bar\Entity\FooBar */<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form </span><span style="color: #007700">= </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">form</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$entity </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">;<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_id'</span><span style="color: #007700">] = array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'textfield'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#title' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'UID'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">user_id</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">target_id</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#size' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">60</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#maxlength' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">128</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#required' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#weight' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">10</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp; );<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'langcode'</span><span style="color: #007700">] = array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Language'</span><span style="color: #007700">),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'language_select'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUntranslated</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">language</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#languages' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">Language</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_ALL</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp; );<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] = array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'hidden'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'#default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEntityTypeId</span><span style="color: #007700">(),<br>&nbsp;&nbsp;&nbsp; );<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$form</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Overrides \Drupal\Core\Entity\EntityFormController::submit().<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">submit</span><span style="color: #007700">(array </span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #FF8000">// Build the entity object from the submitted values.<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$entity </span><span style="color: #007700">= </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">submit</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setRedirect</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo_bar.list'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$entity</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Overrides Drupal\Core\Entity\EntityFormController::save().<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">save</span><span style="color: #007700">(array </span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$entity </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">;<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">save</span><span style="color: #007700">();<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>


foo_bar/src/Entity/Form/FooBarDeleteForm.php

Confirmation form when deleting foo_bar content

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Contains \Drupal\foo_bar\Entity\Form\FooBarDeleteForm<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">ContentEntityConfirmFormBase</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Url</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">\</span><span style="color: #0000BB">FormStateInterface</span><span style="color: #007700">;<br></span><span style="color: #FF8000">/**<br>&nbsp;* Provides a form for deleting a foo_bar entity.<br>&nbsp;*/<br></span><span style="color: #007700">class </span><span style="color: #0000BB">FooBarDeleteForm </span><span style="color: #007700">extends </span><span style="color: #0000BB">ContentEntityConfirmFormBase </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getQuestion</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Are you sure you want to delete entity %name?'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'%name' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label</span><span style="color: #007700">()));<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getCancelUrl</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return new </span><span style="color: #0000BB">Url</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo_bar.list'</span><span style="color: #007700">);<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getConfirmText</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Delete'</span><span style="color: #007700">);<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">submit</span><span style="color: #007700">(array </span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">();<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">watchdog</span><span style="color: #007700">(</span><span style="color: #DD0000">'content'</span><span style="color: #007700">, </span><span style="color: #DD0000">'@type: deleted %title.'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'@type' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bundle</span><span style="color: #007700">(), </span><span style="color: #DD0000">'%title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label</span><span style="color: #007700">()));<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setRedirect</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo_bar.list'</span><span style="color: #007700">);<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>


foo_bar/src/Entity/Controller/FooBarListBuilder.php

Define header and row content for the FooBar listing. The 'Operations' links are added automatically from the 'links' definition in the entityType annotation when the parent functions are called.

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Contains \Drupal\foo_bar\Entity\Controller\FooBarListBuilder.<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">Controller</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityInterface</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">EntityListBuilder</span><span style="color: #007700">;<br></span><span style="color: #FF8000">/**<br>&nbsp;* Provides a list controller for foo_bar entity.<br>&nbsp;*/<br></span><span style="color: #007700">class </span><span style="color: #0000BB">FooBarListBuilder </span><span style="color: #007700">extends </span><span style="color: #0000BB">EntityListBuilder </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">buildHeader</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$header</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">] = </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'FooBarID'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$header</span><span style="color: #007700">[</span><span style="color: #DD0000">'label'</span><span style="color: #007700">] = </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Label'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$header</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo-bar_field'</span><span style="color: #007700">] = </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'FooBarField'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$header </span><span style="color: #007700">+ </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">buildHeader</span><span style="color: #007700">();<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * {@inheritdoc}<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">buildRow</span><span style="color: #007700">(</span><span style="color: #0000BB">EntityInterface $entity</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #FF8000">/* @var $entity \Drupal\foo_bar\Entity\FooBar */<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">();<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'label'</span><span style="color: #007700">] = </span><span style="color: #0000BB">l</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getLabel</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity</span><span style="color: #007700">), </span><span style="color: #DD0000">'foo-bar/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">());<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo_bar_field'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getFooBarField</span><span style="color: #007700">();<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$row </span><span style="color: #007700">+ </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">buildRow</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity</span><span style="color: #007700">);<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>


Field Settings

foo_bar/src/Entity/Form/FooBarSettingsForm.php

Create a settings form for FooBar. Fields can be manage from here.

<span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* @file<br>&nbsp;* Defines Drupal\foo_bar\Entity\Form\FooBarSettingsForm.<br>&nbsp;*/<br></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">foo_bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Entity</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">\</span><span style="color: #0000BB">FormBase</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">Core</span><span style="color: #007700">\</span><span style="color: #0000BB">Form</span><span style="color: #007700">\</span><span style="color: #0000BB">FormStateInterface</span><span style="color: #007700">;<br>class </span><span style="color: #0000BB">FooBarSettingsForm </span><span style="color: #007700">extends </span><span style="color: #0000BB">FormBase </span><span style="color: #007700">{<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Returns a unique string identifying the form.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @return string<br>&nbsp;&nbsp; *&nbsp;&nbsp; The unique string identifying the form.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">getFormId</span><span style="color: #007700">() {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #DD0000">'foo_bar_settings'</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Form submission handler.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @param array $form<br>&nbsp;&nbsp; *&nbsp;&nbsp; An associative array containing the structure of the form.<br>&nbsp;&nbsp; * @param array $form_state<br>&nbsp;&nbsp; *&nbsp;&nbsp; An associative array containing the current state of the form.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">submitForm</span><span style="color: #007700">(array &amp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #FF8000">// Empty implementation of the abstract submit class.<br>&nbsp; </span><span style="color: #007700">}<br>&nbsp; </span><span style="color: #FF8000">/**<br>&nbsp;&nbsp; * Define the form used for FooBar settings.<br>&nbsp;&nbsp; * @return array<br>&nbsp;&nbsp; *&nbsp;&nbsp; Form definition array.<br>&nbsp;&nbsp; *<br>&nbsp;&nbsp; * @param array $form<br>&nbsp;&nbsp; *&nbsp;&nbsp; An associative array containing the structure of the form.<br>&nbsp;&nbsp; * @param array $form_state<br>&nbsp;&nbsp; *&nbsp;&nbsp; An associative array containing the current state of the form.<br>&nbsp;&nbsp; */<br>&nbsp; </span><span style="color: #007700">public function </span><span style="color: #0000BB">buildForm</span><span style="color: #007700">(array </span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo_bar_settings'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'Settings form for FooBar. Manage field settings here.'</span><span style="color: #007700">;<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$form</span><span style="color: #007700">;<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span>