Commit 98d71715 authored by plach's avatar plach

Issue #2935932 by Sam152, dwkitchen, jibran, amateescu, kiamlaluno,...

Issue #2935932 by Sam152, dwkitchen, jibran, amateescu, kiamlaluno, tstoeckler, joachim: Add a FieldDefinition class for defining bundle fields in code
parent dd571111
......@@ -7,6 +7,7 @@
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -405,6 +406,8 @@ protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $
if ($field_definition instanceof BaseFieldDefinition) {
$field_definition->setName($field_name);
$field_definition->setTargetEntityTypeId($entity_type_id);
}
if ($field_definition instanceof BaseFieldDefinition || $field_definition instanceof FieldDefinition) {
$field_definition->setTargetBundle($bundle);
}
}
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Render\Element;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\NodeType;
......@@ -1860,6 +1861,7 @@ function hook_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityT
* @see hook_entity_field_storage_info_alter()
* @see hook_entity_bundle_field_info_alter()
* @see \Drupal\Core\Field\FieldDefinitionInterface
* @see \Drupal\Core\Field\FieldDefinition
* @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
*
* @todo WARNING: This hook will be changed in
......@@ -1869,12 +1871,12 @@ function hook_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $
// Add a property only to nodes of the 'article' bundle.
if ($entity_type->id() == 'node' && $bundle == 'article') {
$fields = [];
$fields['mymodule_text_more'] = BaseFieldDefinition::create('string')
->setLabel(t('More text'))
->setComputed(TRUE)
->setClass('\Drupal\mymodule\EntityComputedMoreText');
$storage_definitions = mymodule_entity_field_storage_info($entity_type);
$fields['mymodule_bundle_field'] = FieldDefinition::createFromFieldStorageDefinition($storage_definitions['mymodule_bundle_field'])
->setLabel(t('Bundle Field'));
return $fields;
}
}
/**
......
This diff is collapsed.
......@@ -8,6 +8,7 @@
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\entity_test\FieldStorageDefinition;
use Drupal\entity_test\Entity\EntityTestMulRev;
......@@ -73,7 +74,9 @@ function entity_schema_test_entity_field_storage_info(EntityTypeInterface $entit
*/
function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
if ($entity_type->id() == 'entity_test' && $bundle == 'custom') {
$definitions['custom_bundle_field'] = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type->id())['custom_bundle_field'];
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $custom_bundle_field_storage */
$custom_bundle_field_storage = entity_schema_test_entity_field_storage_info($entity_type)['custom_bundle_field'];
$definitions[$custom_bundle_field_storage->getName()] = FieldDefinition::createFromFieldStorageDefinition($custom_bundle_field_storage);
return $definitions;
}
}
......@@ -83,11 +86,7 @@ function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity
*/
function entity_schema_test_entity_bundle_create($entity_type_id, $bundle) {
if ($entity_type_id == 'entity_test' && $bundle == 'custom') {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$field_definitions = entity_schema_test_entity_bundle_field_info($entity_type, $bundle);
$field_definitions['custom_bundle_field']
->setTargetEntityTypeId($entity_type_id)
->setTargetBundle($bundle);
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle);
// Notify the entity storage that we just created a new field.
\Drupal::entityManager()->onFieldDefinitionCreate($field_definitions['custom_bundle_field']);
}
......@@ -98,13 +97,9 @@ function entity_schema_test_entity_bundle_create($entity_type_id, $bundle) {
*/
function entity_schema_test_entity_bundle_delete($entity_type_id, $bundle) {
if ($entity_type_id == 'entity_test' && $bundle == 'custom') {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$field_definitions = entity_schema_test_entity_bundle_field_info($entity_type, $bundle);
$field_definitions['custom_bundle_field']
->setTargetEntityTypeId($entity_type_id)
->setTargetBundle($bundle);
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle);
// Notify the entity storage that our field is gone.
\Drupal::entityManager()->onFieldDefinitionDelete($field_definitions['custom_bundle_field']);
\Drupal::entityManager()->onFieldStorageDefinitionDelete($field_definitions['custom_bundle_field']);
\Drupal::entityManager()->onFieldStorageDefinitionDelete($field_definitions['custom_bundle_field']->getFieldStorageDefinition());
}
}
......@@ -80,7 +80,7 @@ public function testCustomBundleFieldUsage() {
$entity->delete();
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $storage->getTableMapping();
$table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'));
$table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field')->getFieldStorageDefinition());
$result = $this->database->select($table, 'f')
->fields('f')
->condition('f.entity_id', $entity->id())
......@@ -93,7 +93,7 @@ public function testCustomBundleFieldUsage() {
$entity->save();
entity_test_delete_bundle('custom');
$table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'), TRUE);
$table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field')->getFieldStorageDefinition(), TRUE);
$result = $this->database->select($table, 'f')
->condition('f.entity_id', $entity->id())
->condition('deleted', 1)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment