By kevinwlu on
Hi,
I feel like I've done a lot of searching around and there's a lot of answers out there using hook_menu_alter, themes, overriding a service, but this is none of the above. The function that I'm trying to tweak is the execute function in views_bulk_edit contributed module in:
/src/Form/BulkEditFormTrait.php
<?php
namespace Drupal\views_bulk_edit\Form;
.
.
.
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$result = $this->t('No values changed');
// Load the edit revision for safe editing.
$entity = $this->entityRepository->getActive($type_id, $entity->id());
if (isset($this->configuration[$type_id][$bundle])) {
$values = $this->configuration[$type_id][$bundle]['values'];
$change_method = $this->configuration[$type_id][$bundle]['change_method'];
/******INSERTED CODE*************/
$token_service = \Drupal::token();
/******INSERTED CODE*************/
foreach ($values as $field => $value) {
//token replace
$entityType = $entity->getEntityType()->getLabel()->render();
/******INSERTED CODE*************/
$value[0]['value'] = $token_service->replace($value[0]['value'], array($entityType => $entity));
/******INSERTED CODE*************/
.
.
.The "INSERTED CODE" comments are the new code. Basically, I'm trying to add token functionality to the views_bulk_edit module.
How do I override this function in my own custom module?
Thanks in advance!
Comments
In this situation, I would
In this situation, I would check whether VBO provides a (post-) execute hook, and then perform your actions there. If no such hook exists, then you can add a submit handler to the form, in a form_alter hook, and then perform your actions there.
An alternative would be to create a new trait, and 'extend' the original trait, override any forms using the trait, and add your own, which would be more of an OOP paradigm, but less of a Drupal paradigm.
Contact me to contract me for D7 -> D10/11 migrations.
Great Suggestsions!
Hi Jaypan,
Really appreciate the suggestions.
Option 1: I might be mistaken, but I don't see a post execute hook.
Option 2: This is what I'm trying now (use a form_alter hook), my custom module's name is coh_user:
This seems to work in terms of replacing the default "submitForm" function (shown below)
So from the looks of it, all I need to do is replicate everything the original submitForm is doing, except add in the minor edit I did in the "execute" function (shown in my original post).
However, I have trouble replicating the views_bulk_edit's original "submitForm" function in my custom module because all the functions and variables that are "$this->..." (shown below) are non-static functions and variables, so as far as I know, these can only be used or accessed with an instantiated object of "BulkEditForm".
And so when I try to copy the code over, I cannot call these functions / variables shown above:
Now, I wish there was someway to fetch the instantiated object of BulkEditForm, but (if i'm correct) it seems that the the hook_form_FORM_ID_alter() function is called before the BulkEditForm object is constructed.
The constructor for BulkEditForm is as below; however, I don't know where I can find all these parameters (entityTypeManager, entityRepository, etc.) to pass in to construct it from my custom module.
I might be going down the wrong rabbit hole. Feel free to offer any re-direction.
Option 3: create a new trait, and 'extend' the original trait, override any forms using the trait, and add your own.
I would like to try this, but how do I go about overriding the original form in my custom module to use my new trait instead of the old one?
Many thanks!