Skip to content
Snippets Groups Projects
Commit 205798e4 authored by Owen Bush's avatar Owen Bush
Browse files

Field inheritance is working

parent c2f58331
No related branches found
No related tags found
No related merge requests found
......@@ -43,8 +43,15 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter
if ($field->entityField()) {
$settings['entity field'] = $field->entityField();
}
// Get field definition to find field type.
$fields[$field->id()] = BaseFieldDefinition::create('string')
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('eventseries', 'eventseries');
$type = 'string';
if (!empty($field_definitions[$field->sourceField()])) {
$type = $field_definitions[$field->sourceField()]->getType();
}
$fields[$field->id()] = BaseFieldDefinition::create($type)
->setLabel($field->label())
->setName($field->id())
->setDescription(t('The inherited field: @field', ['@field' => $field->label()]))
......
......@@ -27,4 +27,11 @@ class FieldInheritance extends Plugin {
*/
public $name;
/**
* An array of field types the inheritance plugin supports.
*
* @var array
*/
public $types = [];
}
......@@ -47,6 +47,7 @@ class FieldInheritanceDeleteForm extends EntityConfirmFormBase {
)
);
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
$form_state->setRedirectUrl($this->getCancelUrl());
}
......
......@@ -108,6 +108,44 @@ class FieldInheritanceForm extends EntityForm {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$values = $form_state->getValues();
if (!empty($values['sourceField']) && !empty($values['entityField'])) {
$series_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('eventseries', 'eventseries');
$instance_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('eventinstance', 'eventinstance');
if ($series_definitions[$values['sourceField']]->getType() !== $instance_definitions[$values['entityField']]->getType()) {
$message = $this->t('Source and entity field definition types must be the same to inherit data. Source - @source_name type: @source_type. Entity - @entity_name type: @entity_type', [
'@source_name' => $values['sourceField'],
'@source_type' => $series_definitions[$values['sourceField']]->getType(),
'@entity_name' => $values['entityField'],
'@entity_type' => $instance_definitions[$values['entityField']]->getType(),
]);
$form_state->setErrorByName('sourceField', $message);
$form_state->setErrorByName('entityField', $message);
}
$plugin_definition = \Drupal::service('plugin.manager.field_inheritance')->getDefinition($values['plugin']);
$field_types = $plugin_definition['types'];
if (!in_array($series_definitions[$values['sourceField']]->getType(), $field_types)) {
$message = $this->t('The selected plugin @plugin does not support @source_type fields. The supported field types are: @field_types', [
'@plugin' => $values['plugin'],
'@source_type' => $series_definitions[$values['sourceField']]->getType(),
'@field_types' => implode(',', $field_types),
]);
$form_state->setErrorByName('sourceField', $message);
$form_state->setErrorByName('plugin', $message);
}
}
}
/**
* {@inheritdoc}
*/
......@@ -127,6 +165,7 @@ class FieldInheritanceForm extends EntityForm {
'%label' => $field_inheritance->label(),
]));
}
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
$form_state->setRedirectUrl($field_inheritance->toUrl('collection'));
}
......
......@@ -3,14 +3,17 @@
namespace Drupal\recurring_events\Plugin\FieldInheritance;
/**
* Text Inheritance plugin.
* String Inheritance plugin.
*
* @FieldInheritance(
* id = "text_inheritance",
* name = @Translation("Text Field Inheritance"),
* id = "string_inheritance",
* name = @Translation("String Field Inheritance"),
* types = {
* "string"
* }
* )
*/
class TextFieldInheritancePlugin extends FieldInheritancePluginBase {
class StringFieldInheritancePlugin extends FieldInheritancePluginBase {
/**
* Concatenation separator.
......
......@@ -8,6 +8,9 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
* @FieldInheritance(
* id = "text_long_inheritance",
* name = @Translation("Text Long Field Inheritance"),
* types = {
* "text_long"
* }
* )
*/
class TextLongFieldInheritancePlugin extends FieldInheritancePluginBase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment