Guest User

Untitled

a guest
May 25th, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.80 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\cig_pods\Form;
  4.  
  5. Use Drupal\Core\Form\FormBase;
  6. Use Drupal\Core\Form\FormStateInterface;
  7.  
  8. class ProjectForm extends FormBase {
  9.  
  10.  
  11.  
  12.     public function getAwardeeOptions(){
  13.         $awardee_assets = \Drupal::entityTypeManager() -> getStorage('asset') -> loadByProperties(
  14.             ['type' => 'awardee']
  15.         );
  16.         $awardee_options = array();
  17.         $awardee_keys = array_keys($awardee_assets);
  18.         foreach($awardee_keys as $awardee_key) {
  19.           $asset = $awardee_assets[$awardee_key];
  20.           $awardee_options[$awardee_key] = $asset->getName();
  21.         }
  22.  
  23.         return $awardee_options;
  24.     }
  25.  
  26.  
  27.     # Making this function dynamically pull contact names breaks AJAX calls.
  28.     public function getAwardeeContactNameOptions(){
  29.         $contact_name_options = array();
  30.         $contact_name_options[''] = ' - Select -';
  31.         $contact_name_options['aw'] = 'Agatha Wallace';
  32.  
  33.         return $contact_name_options;
  34.     }
  35.  
  36.     // TODO: improvement: Make this dynamic
  37.     public function getAwardeeContactTypeOptions(){
  38.         $contact_type_options[''] = '- Select -';
  39.         $contact_type_options['0'] = 'Awardee Administrative Contact';
  40.         $contact_type_options['1'] = 'Awardee Principal Contact';
  41.         $contact_type_options['2'] = 'Awardee Technical Contact';
  42.  
  43.  
  44.         return $contact_type_options;
  45.     }
  46.    
  47.     public function getResourceConcernOptions(){
  48.         $resource_concern_options = [];
  49.         $resource_concern_terms = \Drupal::entityTypeManager() -> getStorage('taxonomy_term') -> loadByProperties(
  50.             ['vid' => 'd_resource_concern']
  51.          );
  52.  
  53.          $resource_concern_keys = array_keys($resource_concern_terms);
  54.  
  55.          foreach($resource_concern_keys as $resource_concern_key) {
  56.            $term = $resource_concern_terms[$resource_concern_key];
  57.            $resource_concern_options[$resource_concern_key] = $term -> getName();
  58.          }
  59.        
  60.         return $resource_concern_options;
  61.     }
  62.  
  63.     public function getGrantTypeOptions(){
  64.  
  65.         $grant_type_terms = \Drupal::entityTypeManager() -> getStorage('taxonomy_term') -> loadByProperties(
  66.             [
  67.                 'vid' => 'd_grant_type',
  68.             ]
  69.         );
  70.  
  71.         $grant_type_options = [];
  72.         $grant_type_keys = array_keys($grant_type_terms);
  73.  
  74.         foreach($grant_type_keys as $grant_type_key) {
  75.             $term = $grant_type_terms[$grant_type_key];
  76.             $grant_type_options[$grant_type_key] = $term -> getName();
  77.         }
  78.         return $grant_type_options;
  79.     }
  80.    
  81.     public function buildProjectInformationSection(array &$form, FormStateInterface &$form_state, $options = NULL){
  82.         $form['form_title'] = [
  83.             '#markup' => '<h1 id="form-title">Project Information</h1>'
  84.         ];
  85.  
  86.         $form['subform_1'] = [
  87.             '#markup' => '<div class="subform-title-container"><h2>General Project Information</h2><h4>7 Fields | Section 1 of 3</h4></div>'
  88.         ];
  89.  
  90.         $form['project_name'] = [
  91.             '#type' => 'textfield',
  92.             '#title' => $this->t('Project Name'),
  93.             '$description' => 'Project Name',
  94.             '#required' => FALSE
  95.         ];
  96.        
  97.         $form['agreement_number'] = [
  98.             '#type' => 'textfield',
  99.             '#title' => $this->t('Agreement Number'),
  100.             '$description' => 'Agreement Number',
  101.             '#required' => FALSE
  102.         ];
  103.  
  104.         // Start: Taxonomy term loaded dynamically example
  105.         $grant_type_options = $this->getGrantTypeOptions();
  106.        
  107.         $form['grant_type'] = [
  108.             '#type' => 'select',
  109.             '#title' => $this
  110.               ->t('Grant Type'),
  111.             '#options' => $grant_type_options,
  112.             '#required' => FALSE
  113.         ];
  114.         // End: Taxonomy term loaded dynamically example
  115.  
  116.         $form['funding_amount'] = [
  117.             '#type' => 'textfield',
  118.             '#title' => $this->t('Funding Amount'),
  119.             '$description' => 'Funding Amount',
  120.             '#required' => FALSE
  121.         ];
  122.  
  123.         $resource_concern_options = $this->getResourceConcernOptions();
  124.         $form['resource_concern'] = [
  125.           '#type' => 'checkboxes',
  126.           '#title' => t('Possible Resource Concerns'),
  127.           '#options' => $resource_concern_options,
  128.           '#required' => FALSE,
  129.           '#required' => FALSE,
  130.         ];
  131.  
  132.         $form['project_summary'] = [
  133.             '#type' => 'textarea',
  134.             '#title' => $this->t('Project Summary'),
  135.             '$description' => 'Project Summary',
  136.             '#required' => FALSE
  137.         ];
  138.  
  139.     }
  140.    /**
  141.    * {@inheritdoc}
  142.    */
  143.     public function buildForm(array $form, FormStateInterface $form_state, $options = NULL){
  144.  
  145.         $form['#attached']['library'][] = 'cig_pods/project_entry_form';
  146.  
  147.         $num_contact_lines = $form_state->get('num_contact_lines');//get num of contacts showing on screen. (1->n exclude:removed indexes)
  148.         $num_contacts = $form_state->get('num_contacts');//get num of added contacts. (1->n)
  149.         $removed_contacts = $form_state->get('removed_contacts');//get removed contacts indexes
  150.  
  151.         if ($num_contacts === NULL) {//initialize number of contact, set to 1
  152.             $form_state->set('num_contacts', 1);
  153.             $num_contacts = $form_state->get('num_contacts');
  154.         }
  155.         if ($num_contact_lines === NULL) {
  156.             $form_state->set('num_contact_lines', 1);
  157.             $num_contact_lines = $form_state->get('num_contact_lines');
  158.         }
  159.  
  160.         if ($removed_contacts === NULL) {
  161.             $form_state->set('removed_contacts', array());//initialize arr
  162.             $removed_contacts = $form_state->get('removed_contacts');
  163.         }
  164.  
  165.  
  166.         $num_producer_lines = $form_state->get('num_producer_lines');
  167.         $num_producers = $form_state->get('num_producers');
  168.         $removed_producers = $form_state->get('removed_producers');
  169.  
  170.         if ($num_producer_lines == NULL){
  171.             $form_state->set('num_producer_lines', 1);
  172.             $num_producer_lines = $form_state->get('num_producer_lines');
  173.         }
  174.  
  175.         if ($num_producers === NULL) {
  176.             $form_state->set('num_producers', 1);
  177.             $num_producers = $form_state->get('num_producers');
  178.         }
  179.  
  180.         if($removed_producers === NULL ){
  181.             $form_state->set('removed_producers', array());
  182.             $removed_producers = $form_state->get('removed_producers');
  183.         }
  184.  
  185.  
  186.  
  187.         /* Variables declaration end*/
  188.  
  189.         $this->buildProjectInformationSection($form, $form_state);
  190.  
  191.         $awardee_options = $this->getAwardeeOptions();
  192.         $contact_name_options = $this->getAwardeeContactNameOptions();
  193.         $contact_type_options = $this->getAwardeeContactTypeOptions();
  194.         /* Awardee Information */
  195.         $form['subform_2'] = [
  196.             '#markup' => '<div class="subform-title-container"><h2>Awardee Information</h2><h4>Section 2215 of 3</h4></div>'
  197.         ];
  198.  
  199.         $form['organization_name'] = [
  200.             '#type' => 'select',
  201.             '#title' => 'Awardee Organization Name',
  202.             '#options' => $awardee_options,
  203.             '#required' => TRUE
  204.         ];
  205.  
  206.  
  207.         // $contact_name_options = $this->getAwardeeContactNameOptions();
  208.         $form['#tree'] = TRUE;
  209.         $form['names_fieldset'] = [
  210.           '#prefix' => '<div id="names-fieldset-wrapper"',
  211.           '#suffix' => '</div>',
  212.         ];
  213.  
  214.        
  215.  
  216.         for ($i = 0; $i < $num_contacts; $i++) {//num_contacts: get num of added contacts. (1->n)
  217.  
  218.             if (in_array($i, $removed_contacts)) {// Check if field was removed
  219.                 continue;
  220.             }
  221.  
  222.             $form['names_fieldset'][$i]['contact_name'] = [
  223.                 '#type' => 'select',
  224.                 '#title' => $this
  225.                   ->t("Contact Name"),
  226.                 '#options' => $contact_name_options,
  227.                 'attributes' => [
  228.                     'class' => 'something',
  229.                 ],
  230.                 '#prefix' => ($num_contact_lines > 1) ? '<div class="inline-components-short">' : '<div class="inline-components">',
  231.                 '#suffix' => '</div>',
  232.             ];
  233.  
  234.             $form['names_fieldset'][$i]['contact_type'] = [
  235.                 '#type' => 'select',
  236.                 '#title' => $this
  237.                   ->t('Contact Type'),
  238.                 '#options' => $contact_type_options,
  239.                 '#prefix' => '<div class="inline-components"',
  240.                 '#suffix' => '</div>',
  241.             ];
  242.  
  243.             if($num_contact_lines > 1 && $i!=0){
  244.                 $form['names_fieldset'][$i]['actions'] = [
  245.                     '#type' => 'submit',
  246.                     '#value' => $this->t('Delete'),
  247.                     '#name' => $i,
  248.                     '#submit' => ['::removeContactCallback'],
  249.                     '#ajax' => [
  250.                       'callback' => '::addContactRowCallback',
  251.                       'wrapper' => 'names-fieldset-wrapper',
  252.                     ],
  253.                     "#limit_validation_errors" => array(),
  254.                     '#prefix' => '<div class="remove-button-container">',
  255.                     '#suffix' => '</div>',
  256.                 ];
  257.             }
  258.  
  259.             //css space for a new line due to previous items' float left attr
  260.             $form['names_fieldset'][$i]['new_line_container'] = [
  261.                 '#markup' => '<div class="clear-space"></div>'
  262.             ];
  263.            
  264.         }
  265.        
  266.         $form['actions'] = [
  267.             '#type' => 'actions',
  268.         ];
  269.  
  270.         $form['names_fieldset']['actions']['add_name'] = [
  271.             '#type' => 'submit',
  272.             '#button_type' => 'button',
  273.             '#name' => 'add_contact_button',
  274.             '#value' => t('Add Another Contact'),
  275.             '#submit' => ['::addContactRow'],
  276.             '#ajax' => [
  277.                 'callback' => '::addContactRowCallback',
  278.                 'wrapper' => 'names-fieldset-wrapper',
  279.             ],
  280.             '#states' => [
  281.                 'visible' => [
  282.                   ":input[name='names_fieldset[0][contact_name]']" => ['!value' => ''],
  283.                   "and",
  284.                   ":input[name='names_fieldset[0][contact_type]']" => ['!value' => ''],
  285.                 ],
  286.             ],
  287.             "#limit_validation_errors" => array(),
  288.             '#prefix' => '<div id="addmore-button-container">',
  289.             '#suffix' => '</div>',
  290.         ];
  291.  
  292.         /* Producers Information */
  293.         $form['subform_3'] = [
  294.             '#markup' => '<div class="subform-title-container"><h2>Producers</h2><h4>Section 5 of 3</h4></div>'
  295.         ];
  296.  
  297.  
  298.         $form['producers_fieldset'] = [
  299.             '#prefix' => '<div id="producers-fieldset-wrapper"',
  300.             '#suffix' => '</div>',
  301.           ];
  302.         for($i = 0; $i < $num_producers; $i++){
  303.             if(in_array($i,$removed_producers)){
  304.                 continue;
  305.             }
  306.            
  307.             $form['producers_fieldset'][$i]['producer_name'] = [
  308.                 '#type' => 'textfield',
  309.                 '#title' => $this
  310.                   ->t("Producer Name"),
  311.             ];
  312.             $form['names_fieldset'][$i]['new_line_container'] = [
  313.                 '#markup' => '<div class="clear-space"></div>'
  314.             ];
  315.         }
  316.         $form['producers_fieldset']['actions']['add_producer'] = [
  317.             '#type' => 'submit',
  318.             '#button_type' => 'button',
  319.             '#name' => 'add_producer_button',
  320.             '#value' => t('Add Another Producer'),
  321.             '#submit' => ['::addProducerRow'],
  322.             '#ajax' => [
  323.                 'callback' => '::addProducerRowCallback',
  324.                 'wrapper' => 'producers-fieldset-wrapper',
  325.             ],
  326.             '#states' => [
  327.                 'visible' => [
  328.                   ":input[name='producers_fieldset[0][producer_name]']" => ['!value' => ''],
  329.                 ],
  330.             ],
  331.             "#limit_validation_errors" => array(),
  332.             '#prefix' => '<div id="addmore-button-container">',
  333.             '#suffix' => '</div>',
  334.         ];
  335.        
  336.         // $form['producer_contact_name'] = [
  337.         //  '#type' => 'textfield',
  338.         //  '#title' => $this->t('Producer Contact Name'),
  339.         //  '$description' => 'Producer Contact Name',
  340.         //  '#required' => TRUE
  341.         // ];
  342.         /* Producers Information Ends*/
  343.  
  344.  
  345.         // $this->buildProducerInformationSection($form, $form_state, $options);
  346.         $form_state->setCached(FALSE);
  347.  
  348.         $form['actions']['save'] = [
  349.             '#type' => 'submit',
  350.             '#value' => $this->t('Save'),
  351.         ];
  352.  
  353.         $form['actions']['cancel'] = [
  354.             '#type' => 'submit',
  355.             '#value' => $this->t('Cancel'),
  356.         ];
  357.  
  358.         return $form;
  359.     }
  360.  
  361.   /**
  362.    * {@inheritdoc}
  363.    */
  364.   public function validateForm(array &$form, FormStateInterface $form_state){
  365.     return;
  366.   }
  367.  
  368.   /**
  369.    * {@inheritdoc}
  370.    */
  371.   public function submitForm(array &$form, FormStateInterface $form_state) {
  372.  
  373.     if($form_state['values']['op'] == t('Save')){
  374.         $this
  375.         ->messenger()
  376.         ->addStatus($this
  377.         ->t('Form submitted for project @project_name', [
  378.             '@project_name' => $form['project_name']['#value'],
  379.         ])
  380.         );
  381.     }
  382.   }
  383.  
  384.   /**
  385.    * {@inheritdoc}
  386.    */
  387.   public function getFormId() {
  388.     return 'project_create_form';
  389.   }
  390.  
  391.   public function addContactRowCallback(array &$form, FormStateInterface $form_state) {
  392.     return $form['names_fieldset'];
  393.   }
  394.   public function addProducerRowCallback(array &$form, FormStateInterface $form_state) {
  395.     return $form['producers_fieldset'];
  396.   }
  397.  
  398.   public function addProducerRow(array &$form, FormStateInterface $form_state){
  399.       $num_producers = $form_state->get('num_producers');
  400.       $num_producer_lines = $form_state->get('num_producer_lines');
  401.       $form_state->set('num_producers', $num_producers + 1);
  402.       $form_state->set('num_producer_lines',$num_producer_lines + 1);
  403.       $form_state->setRebuild();
  404.   }
  405.  
  406.  
  407.   public function addContactRow(array &$form, FormStateInterface $form_state) {
  408.     $num_field = $form_state->get('num_contacts');
  409.     $num_line = $form_state->get('num_contact_lines');
  410.     $add_button = $num_field + 1;
  411.     $form_state->set('num_contacts', $add_button);
  412.     $form_state->set('num_contact_lines', $num_line + 1);
  413.     $form_state->setRebuild();
  414.   }
  415.  
  416.   public function removeContactCallback(array &$form, FormStateInterface $form_state) {
  417.     $trigger = $form_state->getTriggeringElement();
  418.     $num_line = $form_state->get('num_contact_lines');
  419.     $indexToRemove = $trigger['#name'];
  420.  
  421.     // Remove the fieldset from $form (the easy way)
  422.     unset($form['names_fieldset'][$indexToRemove]);
  423.  
  424.     // Keep track of removed fields so we can add new fields at the bottom
  425.     // Without this they would be added where a value was removed
  426.     $removed_contacts = $form_state->get('removed_contacts');
  427.     $removed_contacts[] = $indexToRemove;
  428.     $form_state->set('removed_contacts', $removed_contacts);
  429.     $form_state->set('num_contact_lines', $num_line - 1);
  430.  
  431.     // Rebuild form_state
  432.     $form_state->setRebuild();
  433.   }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment