-2

I have the following form submission handler.

function mymod_form_submit(&$form, FormStateInterface $form_state) {
  $ka_selected = $form_state->getValue('ka_types');
  \Drupal::state()->set('ka_types', $ka_selected);
  return $ka_selected;
}

It returns the ka_types field value. I want to use $ka_selected in custom action code.

public function execute(ContentEntityInterface $entity = NULL) {
    //Read all the fields from entity
    $comment = $entity->get('comment_body')->getValue();
    $file_uri = file_create_url($entity->field_attachment->entity->getFileUri());
    $file_name = $entity->field_attachment->entity->getFileName();
    $ka = $entity->ka_selected->entity->getValue(); **//error thrown here**
    dd($ka);
}

I am able to read other view entities but not just this field.

How can I use the value of $ka_selected in my custom action?

4
  • Could you please clarify - I think there's no hook_form_submit in Drupal 9? And why do you store your value in a state and later expect it in the entity? Commented Jul 7, 2021 at 6:08
  • Drupal 9 has hook_form_submit and I can dump the selected values there perfectly. Just those values are not getting passed on to the execute method in the custom action
    – Jiah
    Commented Jul 7, 2021 at 13:37
  • Drupal has never used hook_form_submit(). A form submission handler isn't a hook. What stored in a form element isn't necessarily an entity field. I could use add a form element in an entity edit form and its value won't end in an entity field.
    – avpaderno
    Commented Jul 7, 2021 at 14:15
  • Also, a form submission handler doesn't return any value. Even if it were to return a value, that value would be discarded from Drupal.
    – avpaderno
    Commented Jul 7, 2021 at 14:16

1 Answer 1

0

Alright, I am able to figure out the answer from the below link: https://www.drupal.org/forum/support/module-development-and-code-questions/2021-07-04/read-view-row-entities-and-create-a#comment-14154305

public function execute(ContentEntityInterface $entity = NULL) {
    //Read all the fields from entity
    $comment = $entity->get('comment_body')->getValue();
    $file_uri = file_create_url($entity->field_attachment->entity->getFileUri());
    $file_name = $entity->field_attachment->entity->getFileName();
    $ka = \Drupal::state()->get('ka_types');
    dd($ka);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.