Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inline_entity_form
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
inline_entity_form
Commits
24227385
Commit
24227385
authored
1 year ago
by
Odai Atieh
Committed by
Andrii Podanenko
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
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
!99
Closes #3359875
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inline_entity_form.api.php
+55
-0
55 additions, 0 deletions
inline_entity_form.api.php
src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
+5
-0
5 additions, 0 deletions
src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
src/WidgetSubmit.php
+3
-0
3 additions, 0 deletions
src/WidgetSubmit.php
with
63 additions
and
0 deletions
inline_entity_form.api.php
+
55
−
0
View file @
24227385
...
@@ -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'
;
}
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
+
5
−
0
View file @
24227385
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
src/WidgetSubmit.php
+
3
−
0
View file @
24227385
...
@@ -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'
]);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment