0

Help me please. How can I get the value of the pressed button. Thanks!

$items = [];

foreach ($result as &$value) {
  $items[] = [
    $form['repo'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('FIELD REPO'),
    ],
    $form['repo']['repo_name'] = [
      '#type' => 'item',
      '#title' => $value->name,
    ],
    $form['repo']['user_name'] = [
      '#type' => 'hidden',
      '#title' => $value->name,
      '#value' => $value->name,
    ],
    $form['repo']['submit'] = [
      '#type' => 'submit',
      '#value' => $value->name,
    ],
  ];
}
$form['repositories_arr'] = [
  '#theme' => 'item_list',
  '#items' => $items,
  '#attributes' => ['id' => 'repositories_arr'],
];




return $form;
  • 1
    None of the above are inputs, so where are you trying to get a value from? – Kevin Oct 17 '19 at 22:01
0

In the foreach loop of your example, you create multiple buttons and you can set a different/distinct '#name' property property to each submit button, e.g.:

foreach ($result as &$value) {
  ...
  $form['repo']['submit'] = [
    '#type' => 'submit',
    '#value' => $value->name,
    '#name' => $value->name,
  ];
  ...
}

In your validation and/or submit handler you can retrieve the element that triggered the form submission using $form_state->getTriggeringElement()['#name'].

| improve this answer | |
  • @Good Media, I'm not sure why you need &$value in your foreach loop and I don't think/understand the assigning part to $form['repo'][<properties>] either. – Watergate Oct 17 '19 at 21:53
  • $form_state->getTriggeringElement()['#name'] This returned last button in my list. Regardless of which button I click – Good Media Oct 18 '19 at 8:00
  • @GoodMedia, did you set distinct values for the '#name' property (of each submit button)? What I would do is create two extra buttons outside your for each and set two values, e.g., '#name' => 'button_1' and '#name' => 'button_2'. In your form submission handler, $form_state->getTriggeringElement()['#name'] should give you 'button_1' when you pressed the first and 'button_2' in case you pressed the second one. – Watergate Oct 18 '19 at 15:58
0

So, what i wanted ...

for ($i = 0; $i < count($arr_git); $i++) {
  $form[] = [
    $item['repo_name'] = [
      '#type' => 'submit',
      '#title' => 'name'.$i,
      '#prefix' => '<div class="form__item">',
      '#attributes' => ['class' => ['item_name']],
    ],
    $item['repo_del'] = [
      '#type' => 'submit',
      '#name' => 'NA::ME___' . $i,
      '#value' => 'Delete',
      '#attributes' => ['class' => ['btn_del']],
      '#suffix' => '</div>',
    ],
  ];
}]

return $form;

This is then what made it work:

$form_state->getTriggeringElement()['#name'];

I rendered the form incorrectly

enter image description here

| improve this answer | |
  • Good to see that you have solved your problem! But I don't think this is a real answer to your (original) question, I recommend you to update your question with the relevant source code and edit (or comment) on the answer that did answer your question. – Watergate Oct 21 '19 at 17:54
  • This method ---> $form_state->getTriggeringElement()['#name'] <--- really search for the pressed button in form and return #name or #value or other attributes, but, this method in my old code from my question return always the last button from my list form. So, I made a decision correct my form. – Good Media Oct 21 '19 at 19:09
  • 1
    No, what made it work was using a different value for '#name' for each submission button, but similar thing should be done for other fields too. – kiamlaluno Oct 22 '19 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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