I am just trying to use existing widgets and formatters for my own lightweight entity, written with fieldable=FALSE.
I don't want to write megatons of FormAPI code and save and constrain (validation) logic for the entity with simple string/text/integer fields > 10.
I implement in mymodule.module:
/**
* Implements hook_field_widget_info_alter().
*/
function mymodule_field_widget_info_alter(&$info) {
$info['text_textfield']['field_types'][] = 'integer';
$info['text_textfield']['field_types'][] = 'string';
}
/**
* Implements hook_field_formatter_info_alter().
*/
function mymodule_field_formatter_info_alter(&$info) {
// Let a new field type re-use an existing formatter.
$info['text_default']['field types'][] = 'integer';
$info['text_default']['field types'][] = 'string';
}
Then define the entity:
/**
* @file
* Contains Drupal\mymodule\Entity\MyEntity.
*/
namespace Drupal\mymodule\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Field\FieldDefinition;
use Drupal\mymodule\myentityInterface;
/**
* Defines the myentity.
*
* @EntityType(
* id = "myentity",
* label = @Translation("myentity"),
* module = "mymodule",
* controllers = {
* "storage" = "Drupal\Core\Entity\FieldableDatabaseStorageController",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "access" = "Drupal\Core\Entity\EntityAccessController",
* "list" = "Drupal\mymodule\myentityListController",
* "form" = {
* "default" = "Drupal\mymodule\myentityFormController",
* },
* },
* base_table = "myentity",
* fieldable = FALSE,
* entity_keys = {
* "id" = "eid",
* "label" = "Name",
* }
* )
*/
class MyEntiy extends ContentEntityBase implements MyEntiyInterface {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions($entity_type) {
$fields['eid'] = FieldDefinition::create('integer')
->setLabel(t('MyEntiy ID'))
->setDescription(t('The MyEntiy ID'))
->setRequired(TRUE)
->setReadOnly(TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The MyEntiy UUID.'))
->setRequired(TRUE)
->setReadOnly(TRUE);
$fields['Name'] = FieldDefinition::create('string')
->setLabel(t('MyEntiy name'))
->setDescription(t('The MyEntiy name.'))
->setRequired(TRUE)
->setSettings(array(
'default_value' => '',
'max_length' => 128,
'text_processing' => 0,
))
->setDisplayOptions('form', array(
'type' => 'text_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE)
->setPropertyConstraints('value', array('Length' => array('max' => 128)));
// ...SOME OTHER FIELDS... //
}
Comments
Comment #1
andypostSuppose this depends on #2010930: [META] Apply formatters and widgets to rendered entity base fields (all besides node.title)
Comment #2
yched commentedTechnically, you can already use widgets / formatters on base fields in your entity types.
The only obstacle is "are there any widget / formatter that work on the field type of my field ?"
This is what #2150511: [meta] Deduplicate the set of available field types is about - but ultimately this is a topic that will need to be adressed field type by field type, with the linked issue working as a META.
I'm kind of looking for someone to lead that effort, TBH - I don't feel I have the bandwidth.
Comment #3
berdir#2100343: Remove 'fieldable' key in entity definitions in favour of 'field_ui_base_route' is kind of related, because then that setting no longer implies anything about fields, just about having configurable fields/extensible storage and not if and how formatters/widgets are applied.
Not a strict dependency, but would still be a nice thing to clean up. Should be a fairly easy patch to write :)
Comment #4
andypostStarting with #2150511: [meta] Deduplicate the set of available field types I found that
EntityFormControllershould build entity form display for all fields from field definition but currently it uses deprecatedfield_attach_form()that attaches only configurable fields.At the same time I think the good question do we need
configurablekey in fieldType annotation because form and view builders should check availability of widgets and formatters to build their arraysComment #5
yched commented@andypost: no, field_attach_form() and _view() alreday attach formatters and widgets for base fields as well. Base fields need to opt in, see how Node::baseFieldDefinition() does it for node.title.
Comment #6
andypostThere's fix and test in #2191555: Allow DisplayOverviewBase use all field types