Skip to content
Snippets Groups Projects
Commit d93f5e14 authored by Pablo López's avatar Pablo López
Browse files

Issue #3526031: Remove the "Add Field Inheritance" feature from the entity form

parent 37799570
No related branches found
No related tags found
1 merge request!19Issue #3526031: Remove the "Add Field Inheritance" feature from the entity form
Pipeline #504460 passed
......@@ -56,9 +56,9 @@ include:
################
variables:
_CSPELL_DICTIONARY: 'field-inheritance-dictionary.txt'
# SKIP_ESLINT: '1'
# OPT_IN_TEST_NEXT_MAJOR: '1'
# _CURL_TEMPLATES_REF: 'main'
OPT_IN_TEST_CURRENT: 1
OPT_IN_TEST_PREVIOUS_MAJOR: 1
###################################################################################
#
......
......@@ -7,15 +7,6 @@ field_inheritance.field_inheritance:
requirements:
_permission: 'administer field inheritance'
# AJAX Field Inheritance creation form.
field_inheritance.creation_form:
path: '/ajax/field_inheritance/creation/{entity_type}/{entity_bundle}'
defaults:
_title: 'Create Field Inheritance'
_controller: '\Drupal\field_inheritance\Controller\FieldInheritanceController::ajaxCreationForm'
requirements:
_permission: 'administer field inheritance'
# Field Inheritance settings admin page.
field_inheritance.settings:
path: '/admin/structure/field_inheritance/settings'
......
<?php
namespace Drupal\field_inheritance\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormState;
/**
* The FieldInheritanceController class.
*/
class FieldInheritanceController extends ControllerBase {
/**
* Gets the creation form in a modal.
*
* @param string $entity_type
* The entity type.
* @param string $entity_bundle
* The entity bundle.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Returns an ajax response.
*/
public function ajaxCreationForm($entity_type = NULL, $entity_bundle = NULL): AjaxResponse {
$inheritance_entity = $this->entityTypeManager()->getStorage('field_inheritance')->create();
$inheritance_entity->setDestinationEntityType($entity_type);
$inheritance_entity->setDestinationEntityBundle($entity_bundle);
$form_object = $this->entityTypeManager()->getFormObject('field_inheritance', 'ajax');
$form_object->setEntity($inheritance_entity);
$form_state = (new FormState())
->setFormObject($form_object)
->disableRedirect();
$modal_form = $this->formBuilder()->buildForm($form_object, $form_state);
$modal_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand('Add Field Inheritance', $modal_form, ['width' => '800']));
return $response;
}
}
......@@ -16,7 +16,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
* "form" = {
* "add" = "Drupal\field_inheritance\Form\FieldInheritanceForm",
* "edit" = "Drupal\field_inheritance\Form\FieldInheritanceForm",
* "ajax" = "Drupal\field_inheritance\Form\FieldInheritanceAjaxForm",
* "delete" = "Drupal\field_inheritance\Form\FieldInheritanceDeleteForm"
* },
* "route_provider" = {
......
<?php
namespace Drupal\field_inheritance\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\ScrollTopCommand;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\field_inheritance\FieldInheritancePluginManager;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* Provides an AJAX form for managing field inheritance entities.
*/
class FieldInheritanceAjaxForm extends FieldInheritanceForm {
use AutowireTrait;
/**
* Construct a FieldInheritanceAjaxForm.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info
* The entity type bundle info service.
* @param \Drupal\field_inheritance\FieldInheritancePluginManager $field_inheritance
* The field inheritance plugin manager.
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder
* The entity form builder service.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer service.
*/
public function __construct(
MessengerInterface $messenger,
EntityTypeManagerInterface $entity_type_manager,
EntityFieldManagerInterface $entity_field_manager,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
#[Autowire(service: FieldInheritancePluginManager::class)]
FieldInheritancePluginManager $field_inheritance,
protected EntityFormBuilderInterface $entityFormBuilder,
protected RendererInterface $renderer,
) {
parent::__construct($messenger, $entity_type_manager, $entity_field_manager, $entity_type_bundle_info, $field_inheritance);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => '::ajaxSubmit',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function ajaxSubmit(array $form, FormStateInterface $form_state): AjaxResponse {
parent::save($form, $form_state);
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
// Grab the values from the form.
$form_values = $form_state->getValues();
// Build an empty entity using the destination entity type and bundle.
$entity = $this->entityTypeManager->getStorage($form_values['destination_entity_type'])->create(['type' => $form_values['destination_entity_bundle']]);
// Build the entity form for the created entity.
$form = $this->entityFormBuilder->getForm($entity, 'default');
// Grab the field inheritance field for the specified entity and add it to
// the parent form.
$field = $form['field_inheritance']['fields']['field_inheritance_' . $form_values['id']];
$response->addCommand(new AppendCommand('#field-inheritance-ajax-container', $field));
// Display the messages generated by the creation of the field inheritance.
$message = [
'#theme' => 'status_messages',
'#message_list' => $this->messenger->all(),
];
// Prevent duplicate messages appearing.
$this->messenger->deleteAll();
// Render the messages.
$messages = $this->renderer->render($message);
$response->addCommand(new HtmlCommand('#field-inheritance-ajax-message', $messages));
$response->addCommand(new ScrollTopCommand('#field-inheritance-ajax-message'));
return $response;
}
}
......@@ -57,10 +57,6 @@ class FieldInheritanceForm extends EntityForm {
$field_inheritance = $this->entity;
assert($field_inheritance instanceof FieldInheritanceInterface);
// This form needs AJAX support.
$form['#attached']['library'][] = 'core/jquery.form';
$form['#attached']['library'][] = 'core/drupal.ajax';
$form['#prefix'] = '<div id="field-inheritance-add-form--wrapper">';
$form['#suffix'] = '</div>';
......@@ -320,10 +316,10 @@ class FieldInheritanceForm extends EntityForm {
],
];
$plugins = [];
foreach ($this->fieldInheritanceManager->getDefinitions() as $plugin_id => $plugin) {
$plugins[$plugin_id] = $plugin['name'];
}
$plugin_definitions = $this->fieldInheritanceManager->getDefinitions();
$plugin_options = array_map(function ($plugin) {
return $plugin['name'];
}, $plugin_definitions);
$global_plugins = [];
$preferred_plugin = '';
......@@ -332,36 +328,36 @@ class FieldInheritanceForm extends EntityForm {
// type.
if (!empty($field_values['source_field'])) {
$source_definitions = $this->entityFieldManager->getFieldDefinitions($field_values['source_entity_type'], $field_values['source_entity_bundle']);
foreach ($plugins as $key => $plugin) {
if ($key === '') {
foreach (array_keys($plugin_options) as $option) {
if ($option === '') {
continue;
}
$plugin_definition = $this->fieldInheritanceManager->getDefinition($key);
$plugin_definition = $plugin_definitions[$option];
$field_types = $plugin_definition['types'];
if (!in_array('any', $field_types)) {
$preferred_plugin = $key;
$preferred_plugin = $option;
if (!in_array($source_definitions[$field_values['source_field']]->getType(), $field_types)) {
unset($plugins[$key]);
unset($plugin_options[$option]);
}
}
// Global plugins should not take precedent over more specific plugins.
if (in_array('any', $field_types)) {
if (empty($preferred_plugin)) {
$preferred_plugin = $key;
$preferred_plugin = $option;
}
$global_plugins[$key] = $plugins[$key];
unset($plugins[$key]);
$global_plugins[$option] = $plugin_options[$option];
unset($plugin_options[$option]);
}
}
// If we have some global plugins, place them at the end of the list.
if (!empty($global_plugins)) {
$plugins = array_merge($plugins, $global_plugins);
$plugin_options = array_merge($plugin_options, $global_plugins);
}
$default_plugins = ['' => $this->t('- Select -')];
if (empty($plugins)) {
$plugins = $default_plugins;
if (empty($plugin_options)) {
$plugin_options = $default_plugins;
}
$form['advanced'] = [
......@@ -374,7 +370,7 @@ class FieldInheritanceForm extends EntityForm {
'#type' => 'select',
'#title' => $this->t('Inheritance Plugin'),
'#description' => $this->t('Select the plugin used to perform the inheritance.'),
'#options' => $plugins,
'#options' => $plugin_options,
'#required' => TRUE,
'#default_value' => $field_inheritance->isNew() ? $preferred_plugin : ($field_inheritance->plugin() ?? $preferred_plugin),
];
......
......@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Drupal\field_inheritance\Hook;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
......@@ -17,7 +16,6 @@ use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field_inheritance\Entity\FieldInheritanceInterface;
use Drupal\field_inheritance\EntityReferenceFieldInheritanceFactory;
......@@ -65,6 +63,11 @@ class FieldInheritanceHooks {
// Main module help for the field_inheritance module.
case 'help.page.field_inheritance':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The field inheritance module can be considered to be a field-level entity reference alternative. It allows site administrators to inherit any field from any entity into any other entity.') . '</p>';
$output .= '<p>' . t('For more information, see the <a href=":docs">online documentation for the Field Inheritance module</a>.', [
':docs' => 'https://www.drupal.org/project/field_inheritance',
]) . '</p>';
return $output;
}
......@@ -208,12 +211,22 @@ class FieldInheritanceHooks {
}
}
$inherited_field_ids = $field_inheritance_storage
->getQuery()
->accessCheck(FALSE)
->condition('destinationEntityType', $entity_type)
->condition('destinationEntityBundle', $bundle)
->execute();
if (empty($inherited_field_ids)) {
return;
}
$state_key = $entity->getEntityTypeId() . ':' . $entity->uuid();
$state = $this->keyValue->get('field_inheritance');
$state_values = $state->get($state_key);
$form['actions']['submit']['#submit'][] = 'field_inheritance_entity_form_submit';
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['field_inheritance'] = [
'#type' => 'details',
'#open' => TRUE,
......@@ -240,13 +253,6 @@ class FieldInheritanceHooks {
],
];
$inherited_field_ids = $field_inheritance_storage
->getQuery()
->accessCheck(FALSE)
->condition('destinationEntityType', $entity_type)
->condition('destinationEntityBundle', $bundle)
->execute();
if (!empty($inherited_field_ids)) {
$inherited_fields = $field_inheritance_storage->loadMultiple($inherited_field_ids);
......@@ -289,42 +295,6 @@ class FieldInheritanceHooks {
}
}
}
$form['field_inheritance']['fields']['message'] = [
'#type' => 'markup',
'#markup' => '<div id="field-inheritance-ajax-message"></div>',
];
$form['field_inheritance']['fields']['ajax_container'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'field-inheritance-ajax-container',
],
];
$form['field_inheritance']['fields']['add'] = [
'#type' => 'fieldset',
'#access' => $this->currentUser->hasPermission('administer field inheritance'),
'message' => [
'#type' => 'markup',
'#prefix' => '<p>',
'#markup' => t('Add a new field inheritance field to this entity type and bundle.'),
'#suffix' => '</p>',
],
'add_field_inheritance' => [
'#type' => 'link',
'#title' => t('Add Field Inheritance'),
'#url' => Url::fromRoute('field_inheritance.creation_form', [
'entity_type' => $entity->getEntityTypeId(),
'entity_bundle' => $entity->bundle(),
]),
'#attributes' => [
'class' => ['use-ajax', 'button', 'button--small'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode(['width' => '60%']),
],
],
];
}
}
}
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\field_inheritance\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for field_inheritance.
*
* @group field_inheritance
*/
class GenericTest extends GenericModuleTestBase {}
<?php
namespace Drupal\Tests\field_inheritance\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group field_inheritance
*/
class LoadTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['field_inheritance'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* Set the default theme to use.
*
* @var \string
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment