SHARE
TWEET

Untitled

a guest Jul 17th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**  
  2.  * Implements hook_field_widget_WIDGET_TYPE_form_alter().  
  3.  */  
  4. function modulename_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  5.  
  6.     $element['subform']['field_course']['widget']['#ajax'] = [
  7.               'callback' => 'get_competency',
  8.               'event' => 'change',
  9.               'method' => 'replace',
  10.               'wrapper' => '#edit-competency-'.$uuid_value,
  11.             ];
  12.  
  13. }
  14.  
  15.  
  16.  
  17. function get_competency(array &$element, FormStateInterface $form_state, $context) {
  18.  
  19.  
  20.     $triggeringElement = $form_state->getTriggeringElement();
  21.     $value = $triggeringElement['#value'];
  22.  
  23.     $gt = Drupal::service('generic_templates.utility');
  24.     $options = $gt->getCompetency($value);
  25.     $options = ["_none" => "- Select a value -"] + $options;
  26.     $wrapper_id = $triggeringElement["#ajax"]["wrapper"];
  27.     unset($triggeringElement["#value"]);
  28.     unset($triggeringElement["#ajax"]);
  29.     $triggeringElement['#title'] = "Competency";
  30.     $triggeringElement['#prefix'] = '<div id="'.$wrapper_id.'">';
  31.     $triggeringElement['#suffix'] = '</div>';
  32.  
  33.     $triggeringElement['#options'] = $options;
  34.     $triggeringElement['#default_value'] = NULL;
  35.     $triggeringElement['#field_name'] = " field_competency";
  36.     $triggeringElement['#parents'] =
  37.             [
  38.                 "field_configurations",
  39.                 0,
  40.                  "subform",
  41.                 "field_configuration",
  42.                  0,
  43.                  "subform",
  44.                  "field_score",
  45.                  0,
  46.                  "subform",
  47.                 "field_competency",
  48.             ];
  49.  
  50.      $triggeringElement["#array_parents"] = [
  51.                  "field_configurations",
  52.                  "widget",
  53.                  0,
  54.                 "subform",
  55.                  "field_configuration",
  56.                 "widget",
  57.                  0,
  58.                 "subform",
  59.                  "field_score",
  60.                 "widget",
  61.                 0,
  62.                  "subform",
  63.                 "field_competency",
  64.                 "widget",
  65.             ];
  66.     $triggeringElement["#attributes"]["data-drupal-selector"] = str_replace("course","competency", $triggeringElement["#attributes"]["data-drupal-selector"]);
  67.     $triggeringElement["#name"] = str_replace("course","competency", $triggeringElement["#name"]);
  68.     $id = explode('--',$triggeringElement["#id"])[0];
  69.     $triggeringElement["#id"] = str_replace("course","competency", $id);
  70.     return $triggeringElement;
  71.  
  72. }
  73.      
  74. use DrupalCoreAjaxAjaxResponse;
  75. use DrupalCoreAjaxHtmlCommand;
  76.  
  77.  
  78. /**  
  79.  * Implements hook_field_widget_WIDGET_TYPE_form_alter().  
  80.  */  
  81. function modulename_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  82.  
  83.     $element['subform']['field_course']['widget']['#ajax'] = [
  84.               'callback' => 'get_competency',
  85.               'event' => 'change',
  86.               'method' => 'html',
  87.               'wrapper' => 'edit-competency-'.$uuid_value,
  88.             ];
  89.  
  90.  $element['subform']['field_competency']['widget']['#attributes']['id']=  'edit-competency-'.$uuid_value;
  91.  
  92. }
  93.  
  94.  
  95.  
  96. function get_competency(array &$element, FormStateInterface $form_state, $context) {
  97.  
  98.     $triggeringElement = $form_state->getTriggeringElement();
  99.     $value = $triggeringElement['#value'];
  100.  
  101.     $gt = Drupal::service('generic_templates.utility');
  102.     $options = $gt->getCompetency($value);
  103.     $options = ["_none" => "- Select a value -"] + $options;
  104.     $wrapper_id = $triggeringElement["#ajax"]["wrapper"];
  105.  
  106.     $gt = Drupal::service('generic_templates.utility');
  107.     $options = $gt->getCompetency($value);
  108.     $options = ["_none" => "- Select a value -"] + $options;
  109.     $renderedField = '';
  110.  
  111.     foreach ($options as $key => $value) {
  112.         $renderedField .= "<option value='".$key."'>".$value."</option>";
  113.     }
  114.  
  115.     $response = new AjaxResponse();
  116.     $response->addCommand(new HtmlCommand('#'.$wrapper_id, $renderedField));
  117.     return $response;
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top