Skip to content
Snippets Groups Projects
Commit 24227385 authored by Odai Atieh's avatar Odai Atieh Committed by Andrii Podanenko
Browse files

Issue #3197629 by JordiK, Odai Atieh: New API hooks to react on inline entity save/delete

parent 35e32b58
No related branches found
No related tags found
1 merge request!99Closes #3359875
...@@ -58,3 +58,58 @@ function hook_inline_entity_form_table_fields_alter(array &$fields, array $conte ...@@ -58,3 +58,58 @@ function hook_inline_entity_form_table_fields_alter(array &$fields, array $conte
]; ];
} }
} }
/**
* React on a IEF widget entity being saved.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the parent form.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being inserted or updated.
* @param bool $is_new
* Shows if we create (true) or update an entity (false).
*/
function hook_inline_entity_form_entity_save(FormStateInterface &$form_state, ContentEntityInterface $entity, $is_new) {
$operation = ($is_new == TRUE) ? 'created' : 'updated';
$message = 'Entity ' . $entity->id() . ' was ' . $operation . ' in parent entity ' . $form_state->getFormObject()->getEntity()->id();
\Drupal::logger('mymodule')->notice($message);
}
/**
* React on a IEF widget entity being deleted.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the parent form.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being deleted.
*/
function hook_inline_entity_form_entity_delete(FormStateInterface &$form_state, ContentEntityInterface $entity) {
$message = 'Entity ' . $entity->id() . ' was deleted in parent entity ' . $form_state->getformObject()->getEntity()->id();
\Drupal::logger('mymodule')->notice($message);
}
/**
* Alter the list of entity bundles, which can be created with IEF widgets.
*
* @param array $create_bundles
* A list of entity bundles.
* @param array $context
* An array with the following keys:
* - parent_entity_type: The type of the parent entity.
* - parent_bundle: The bundle of the parent entity.
* - field_name: The name of the reference field on which IEF is operating.
* - entity_type: The type of the referenced entities.
* - allowed_bundles: Bundles allowed on the reference field.
*
* @see \Drupal\inline_entity_form\InlineFormInterface::getTableFields()
*/
function hook_inline_entity_form_create_bundles_alter(array &$create_bundles, array $context) {
// Remove a the possibility to create cost items.
if ($context['parent_entity_type'] == 'budget') {
if (($key = array_search('budget_cost_item', $create_bundles)) !== FALSE) {
unset($create_bundles[$key]);
}
}
// Add a revenue item to the list of bundles to create.
$create_bundles[] = 'budget_revenue_item';
}
...@@ -538,6 +538,11 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF ...@@ -538,6 +538,11 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
} }
$create_bundles = $this->getCreateBundles(); $create_bundles = $this->getCreateBundles();
// Let modules modify the create_bundles array.
$context['widget'] = $this;
$this->moduleHandler->alter('inline_entity_form_create_bundles', $create_bundles, $context);
$create_bundles_count = count($create_bundles); $create_bundles_count = count($create_bundles);
$allow_new = $settings['allow_new'] && !empty($create_bundles); $allow_new = $settings['allow_new'] && !empty($create_bundles);
$hide_cancel = FALSE; $hide_cancel = FALSE;
......
...@@ -51,16 +51,19 @@ class WidgetSubmit { ...@@ -51,16 +51,19 @@ class WidgetSubmit {
if (!empty($entity_item['entity']) && !empty($entity_item['needs_save'])) { if (!empty($entity_item['entity']) && !empty($entity_item['needs_save'])) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_item['entity']; $entity = $entity_item['entity'];
$is_new = $entity->isNew();
$handler = InlineEntityForm::getInlineFormHandler($entity->getEntityTypeId()); $handler = InlineEntityForm::getInlineFormHandler($entity->getEntityTypeId());
$referenceUpgrader->upgradeEntityReferences($entity); $referenceUpgrader->upgradeEntityReferences($entity);
$handler->save($entity); $handler->save($entity);
$referenceUpgrader->registerEntity($entity); $referenceUpgrader->registerEntity($entity);
$entity_item['needs_save'] = FALSE; $entity_item['needs_save'] = FALSE;
\Drupal::moduleHandler()->invokeAll('inline_entity_form_entity_save', [&$form_state, $entity, $is_new]);
} }
} }
/** @var \Drupal\Core\Entity\ContentEntityInterface $entities */ /** @var \Drupal\Core\Entity\ContentEntityInterface $entities */
foreach ($widget_state['delete'] as $entity) { foreach ($widget_state['delete'] as $entity) {
\Drupal::moduleHandler()->invokeAll('inline_entity_form_entity_delete', [&$form_state, $entity]);
$entity->delete(); $entity->delete();
} }
unset($widget_state['delete']); unset($widget_state['delete']);
......
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