0

I have created custom fields using field,field widget and field formatters. One of the field I have created is a button. What I need is to write the submit handler for that button.

This is my CertfieldDefaultWidget.php


namespace Drupal\certfield\Plugin\Field\FieldWidget;

use Drupal;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
 * Plugin implementation of the 'CertfieldDefaultWidget' widget.
 *
 * @FieldWidget(
 *   id = "CertfieldDefaultWidget",
 *   label = @Translation("Certfield select"),
 *   field_types = {
 *     "Certfield"
 *   }
 * )
 */
class CertfieldDefaultWidget extends WidgetBase {
  /**
   * Define the form for the field type.
   * 
   * Inside this method we can define the form used to edit the field type.
   * 
   * Here there is a list of allowed element types: https://goo.gl/XVd4tA
   */
  public function formElement(
    FieldItemListInterface $items,
    $delta, 
    Array $element, 
    Array &$form, 
    FormStateInterface $formState
  ) {
      //print_r($items[$delta]->cert_fqdn);
      //exit();


 $element['cert_fqdn'] = [
      '#type' => 'textfield',
      '#title' => t('Fully qualified Domain Name'),
      '#value' => isset($items[$delta]->cert_fqdn) ? 
          $items[$delta]->cert_fqdn : null,
      '#empty_value' => '',
      '#placeholder' => t('Fully qualified Domain Name'),
    ];
    $element['privatekey'] = [
      '#type' => 'textfield',
      '#title' => t('Private Key'),

      // Set here the current value for this field, or a default value (or 
      // null) if there is no a value
      '#default_value' => isset($items[$delta]->cert_key) ? 
          $items[$delta]->cert_key : null,

      '#empty_value' => '',
      '#placeholder' => t('PrivateKey'),
    ];

    // City

    $element['csr'] = [
      '#type' => 'textfield',
      '#title' => t('Certificate Signing Request'),
      '#default_value' => isset($items[$delta]->cert_csr) ? 
          $items[$delta]->cert_csr : null,
      '#empty_value' => '',
      '#placeholder' => t('Certificate Signing Request'),
    ];
    $element['cert_crt'] = [
      '#type' => 'textfield',
      '#title' => t('Server Certificate'),
      '#placeholder' => t('Server Certificate'),
    ];
    $element['create_cert_btn'] = [
      '#type' => 'submit',
      '#title' => t('Server Certificate'),
      '#placeholder' => t('Server Certificate'),
    ]; 

$element['create_cert_btn'] = array(
    //'#name' => implode('_', $element['#parents']) .'_create_cert_btn',
    '#type' => 'submit',
    '#value' => t('Create/Renew Cert'),
    '#callback' => '_certfield_button_action'
        );

    return $element;
  }


} // class
?>

In my certfield.module OR CertfieldDefaultWidget.php file I have a function like as below

function _certfield_button_action(&$element, $form_state, $button){
}

But none of them is called when clicks on the Button .Please help me to find out a solution .

  • Are you trying to submit the entire form in a field widget? That seems like a bad idea, and I'm not sure why all of the answers are suggesting to do this. Or are you trying to invoke an AJAX callback from a button on the field widget? If you're trying to add something to form state, this is usually done in validate and setting a value into $form_state. – mradcliffe Aug 16 '16 at 16:13
  • No I am not trying to submit form. My requirement is to generate a Key on that button click. I have defined a function to generate key.So I need to call that function on button click. – Jitha M Saroj Aug 17 '16 at 4:16
1

Because this is your custom widget, you don't need to create a custom submit function.

Just add WidgetBase::massageFormValues method to your class:

class CertfieldDefaultWidget extends WidgetBase {
  ....
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    // manipulate with $values where $values is an array of your fields defined in your formElement.
    return $values;
  }
  ....
}
-1

I'm not certain, but can't you add a submit handler to $form?

Something like $form['submit'][] = '_certfield_button_action'

  • Thanks. But this not a form like structure – Jitha M Saroj Aug 16 '16 at 11:47
-1

In your widget, replace

'#callback' => '_certfield_button_action'

by

'#submit' => [[get_class($this), 'certfieldButtonAction']]

and implement

public static function certfieldButtonAction(array $form, FormStateInterface $form_state)
  • I need to pass $element and $form_state to the function. – Jitha M Saroj Aug 17 '16 at 4:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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