0

I have made a content type in /admin/structure/types/add and then, created for this content type extra fields from /admin/structure/types/manage/CONTENT TYPE_NAME/fields/add-field. After i created a module that will alter this content type by hook_form_BASE_FORM_ID_alter() in .MODULE file, and after i reset the default button that has content type that is SAVE button, and added new button:

    $form['register'] = array
  (
    '#type' => 'submit',
    '#value' => t('Register'),
    '#submit' => array('NAME_OF_A_FUNCTION'),
    '#weight' => '?',
  );

But when I do this the button is rendered first and after comes the content type fields. How to change the button row weight so that the button will always will show at the last plaice. In aver words how to get from all fields that has content type form and search for the biggest weight that has field and put this 'value + 1' in my new submit button '#weight' => '?'. Dose anyone has solution for that... Thanks in advance

1 Answer 1

1

the solution is in this code:

  $elements = Element::getVisibleChildren($form);
  $weight = 0;
  foreach ($elements as $el) {
    $check = strpos($el, 'field_');
    if ($check !== FALSE && $form[$el]['#weight'] >= $weight) {
      $weight = $form[$el]['#weight'];
    }
  }


  $form['register'] = [
    '#type' => 'submit',
    '#value' => t('Register'),
    '#submit' => array('NAME_OF_A_FUNCTION'),
    '#weight' => $weight + 1,
  ];

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.