0

I have created a custom field widget

my CertfieldDefaultWidget.php is as follows

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 {
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
         $node = \Drupal::routeMatch()->getParameter('node');
        $fqdn= $node->field_p_url->value;
   $pvtkey = $_SESSION['certfield']['privatekey'];
   $csr= $_SESSION['certfield']['csr'];

     // Set up the form element for this widget.
    $element += array(
      '#type' => 'container',
      '#element_validate' => array(
        array($this, 'validate'),
      ),
    );

    // Add in the RGB textfield elements.
    foreach (array('cert_fqdn' => t('Fully qualified Domain Name') ) as $key => $title) {
      $element[$key] = array(
        '#type' => 'textfield',
        '#disabled' => 'disabled',
        '#title' => $title,
        '#size' => 64,
       '#default_value' => $fqdn,
        '#attributes' => array('class' => array('rgb-entry')),

      );
      // Since Form API doesn't allow a fieldset to be required, we
      // have to require each field element individually.
      if ($element['#required']) {
        $element[$key]['#required'] = TRUE;
      }
    }
    $element['privatekey'] = array(
    //'#name' => implode('_', $element['#parents']) .'_create_cert_btn',
    '#type' => 'textarea',
        '#title' => 'Private Key',
        '#name'=>'privatekey',
    '#default_value' => $pvtkey,

        //'#post' => $element['#post'],

    );
    $element['csr'] = array(
    //'#name' => implode('_', $element['#parents']) .'_create_cert_btn',
    '#type' => 'textarea',
        '#title' => 'Certificate Signing Request',
        '#name'=>'csr',
    '#default_value' => $csr,

        //'#post' => $element['#post'],

    );
     $element['sc'] = array(
    //'#name' => implode('_', $element['#parents']) .'_create_cert_btn',
    '#type' => 'textarea',
        '#title' => 'Server Certificate',
        '#name'=>'sc',
      );
    $element['create_cert_btn'] = array(
    //'#name' => implode('_', $element['#parents']) .'_create_cert_btn',
    '#type' => 'button',
        '#name'=>'create_but',
    '#value' => t('Create/Renew Cert'),

        //'#post' => $element['#post'],

    );
    return array('value' => $element);

  }
 /* public function _get_fqdn($element, $form_state) {
     echo "in get fqdn";
     exit();
  $fqdn_field = $element['fqdnfield']['#value'];
  $fqdn_value = $form_state['values'][$fqdn_field][0]['value'];
  // Sanitize and check the url
  //kpr(strtolower(preg_replace('/^.*:\/\/(.*)(:.*)?/', '$1', $fqdn_value)));
  // Cut off http:// and :, convert to lowercase.
  return strtolower(preg_replace('/^(.*:\/\/)?(.*)(:.*)?/', '$2', $fqdn_value));
}*/

public function validate($element, FormStateInterface $form_state) {


    $values = array();
    foreach (array('cert_fqdn', 'privatekey', 'csr' ,'sc','create_cert_btn') as $colorfield) {
      $values[$colorfield] = $element[$colorfield]['#value'];
      //echo $values[$colorfield];
      //exit();
      // If they left any empty, we'll set the value empty and quit.


    }
    $but=$values['create_cert_btn'];
  // echo $but;


$configargs = array(
        'config' => dirname(__FILE__) .'/openssl.cnf',
        'digest_alg' => 'sha1',
        'private_key_bits' => 2048,
        'private_key_type' => OPENSSL_KEYTYPE_RSA,
        'encrypt_key' => false,
        );

    $dn = array(
        "countryName" => "DE",
        "stateOrProvinceName" => "NRW",
        "localityName" => "Ratingen",
        "organizationName" => "Vodafone GIS",
        "organizationalUnitName" => "EWA",
        "commonName" => $values['cert_fqdn'],
        );


    $privkey = null;
    // Generate a certificate signing request. Key is generated in one step.
    $csr = openssl_csr_new($dn, $privkey, $configargs);

    openssl_csr_export($csr, $csrout);
        $element['csr']['#default_value']=$csrout;
        //var_dump($csrout);

    openssl_pkey_export($privkey, $pkeyout, '', $configargs);
        //$element['privatekey']['#default_value']=$pkeyout;
         $values['privatekey']=$pkeyout;
        //var_dump($pkeyout);
        $value = strtolower(sprintf('#%02s%02s', $pkeyout, $csrout));
        //print_r($value);

         $_SESSION['certfield']['privatekey'] = $pkeyout;
         $_SESSION['certfield']['csr'] = $csrout;

        $form_state->setValueForElement($element, $value);
        drupal_set_message('The CSR has been created, make sure to save before proceeding!');

  }




} // class

I need to access the values in that widget form for that in certfield.module i have used hook_widget_alter

function certfield_field_widget_form_alter(&$element, &$form_state, $context)
{

}

Have checked this also 'https://drupal.stackexchange.com/questions/187787/alter-field-not-work-in-hook-field-widget-form-alter-function' Please help me to access the values I entered in Custom widget form.

  • It is a little difficult to read your code example. It would be easier if you could pick a coding standard to adhere to. I think most would prefer the Drupal/PEAR standard, but I don't think it's absolutely necessary. – mradcliffe Aug 25 '16 at 13:15
  • This is very old, but still an interesting topic. Here a link to achieve this. activelamp.com/blog/drupal/custom-fields-part-1 – JF Kiwad Aug 15 '17 at 3:48

Your Answer

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

Browse other questions tagged or ask your own question.