Guest User

Untitled

a guest
May 25th, 2022
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.97 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.         Take in form and form_state using pass by reference on the form. No need to return the form
  13.     */
  14.     public function buildProjectInformationSection(array &$form, FormStateInterface &$form_state, $options = NULL){
  15.         $form['form_title'] = [
  16.             '#markup' => '<h1 id="form-title">Project Information</h1>'
  17.         ];
  18.  
  19.         $form['subform_1'] = [
  20.             '#markup' => '<div class="subform-title-container"><h2>General Project Information</h2><h4>7 Fields | Section 1 of 3</h4></div>'
  21.         ];
  22.  
  23.         $form['project_name'] = [
  24.             '#type' => 'textfield',
  25.             '#title' => $this->t('Project Name'),
  26.             '$description' => 'Project Name',
  27.             '#required' => FALSE
  28.         ];
  29.        
  30.         $form['agreement_number'] = [
  31.             '#type' => 'textfield',
  32.             '#title' => $this->t('Agreement Number'),
  33.             '$description' => 'Agreement Number',
  34.             '#required' => FALSE
  35.         ];
  36.  
  37.         $form['funding_amount'] = [
  38.             '#type' => 'textfield',
  39.             '#title' => $this->t('Funding Amount'),
  40.             '$description' => 'Funding Amount',
  41.             '#required' => FALSE
  42.         ];
  43.  
  44.         $resource_concern_terms = \Drupal::entityTypeManager() -> getStorage('taxonomy_term') -> loadByProperties(
  45.            ['vid' => 'd_resource_concern']
  46.         );
  47.        
  48.  
  49.  
  50.  
  51.         $resource_concern_options = [];
  52.         $resource_concern_keys = array_keys($resource_concern_terms);
  53.  
  54.         foreach($resource_concern_keys as $resource_concern_key) {
  55.           $term = $resource_concern_terms[$resource_concern_key];
  56.           $resource_concern_options[$resource_concern_key] = $term -> getName();
  57.         }
  58.         $form['resource_concern'] = [
  59.           '#type' => 'checkboxes',
  60.           '#title' => t('Possible Resource Concerns'),
  61.           '#options' => $resource_concern_options,
  62.           '#required' => FALSE,
  63.           '#required' => FALSE,
  64.         ];
  65.  
  66.         $form['project_summary'] = [
  67.             '#type' => 'textarea',
  68.             '#title' => $this->t('Project Summary'),
  69.             '$description' => 'Project Summary',
  70.             '#required' => FALSE
  71.         ];
  72.  
  73.         // Start: Taxonomy term loaded dynamically example
  74.  
  75.         $grant_type_terms = \Drupal::entityTypeManager() -> getStorage('taxonomy_term') -> loadByProperties(
  76.             [
  77.                 'vid' => 'd_grant_type',
  78.             ]
  79.         );
  80.  
  81.         $grant_type_options = [];
  82.         $grant_type_keys = array_keys($grant_type_terms);
  83.  
  84.         foreach($grant_type_keys as $grant_type_key) {
  85.             $term = $grant_type_terms[$grant_type_key];
  86.             $grant_type_options[$grant_type_key] = $term -> getName();
  87.         }
  88.        
  89.         $form['grant_type'] = [
  90.             '#type' => 'select',
  91.             '#title' => $this
  92.               ->t('Grant Type'),
  93.             '#options' => $grant_type_options,
  94.             '#required' => FALSE
  95.         ];
  96.         // End: Taxonomy term loaded dynamically example
  97.  
  98.  
  99.     }
  100.  
  101.     /*
  102.         Take in form and form_state using pass by reference on the form. No need to return the form
  103.     */
  104.     public function buildProducerInformationSection(array &$form, FormStateInterface &$form_state, $options = NULL){
  105.         /* Producers Information */
  106.         $form['subform_3'] = [
  107.             '#markup' => '<div class="subform-title-container"><h2>Producers</h2><h4>Section 3 of 3</h4></div>'
  108.         ];
  109.  
  110.         $form['producer_contact_name'] = [
  111.             '#type' => 'textfield',
  112.             '#title' => $this->t('Producer Contact Name'),
  113.             '$description' => 'Producer Contact Name',
  114.             '#required' => FALSE
  115.         ];
  116.         /* Producers Information Ends*/
  117.     }
  118.  
  119.    /**
  120.    * {@inheritdoc}
  121.    */
  122.     public function buildForm(array $form, FormStateInterface $form_state, $options = NULL){
  123.  
  124.         $form['#attached']['library'][] = 'cig_pods/project_entry_form';
  125.  
  126.  
  127.         $this->buildProjectInformationSection($form, $form_state, $options);
  128.        
  129.         $form_state->set('num_indexes', $form_state->get('num_indexes')       == NULL ? 1 : $form_state->get('num_indexes') );
  130.         $form_state->set('num_lines', $form_state->get('num_lines')           == NULL ? 1 : $form_state->get('removed_fields') );
  131.         $form_state->set('removed_fields', $form_state->get('removed_fields') == NULL ? array() : $form_state->get('removed_fields') );
  132.  
  133.         $num_indexes = $form_state->get('num_indexes');
  134.         $num_lines = $form_state->get('num_lines');
  135.         $removed_fields = $form_state->get('removed_fields');
  136.  
  137.         // $removed_fields = $form_state->get('removed_fields');//get removed contacts indexes
  138.         /* Variables declaration end*/
  139.  
  140.         /* Awardee Information */
  141.         $form['subform_2'] = [
  142.             '#markup' => '<div class="subform-title-container"><h2>Awardee Information</h2><h4>Section 2 of 3</h4></div>'
  143.         ];
  144.  
  145.         /* Selection of existing Awardees*/
  146.  
  147.         $awardee_assets = \Drupal::entityTypeManager() -> getStorage('asset') -> loadByProperties(
  148.             ['type' => 'awardee']
  149.         );
  150.         $awardee_options = array();
  151.         $awardee_keys = array_keys($awardee_assets);
  152.         print_r($awardee_keys);
  153.         foreach($awardee_keys as $awardee_key) {
  154.           $asset = $awardee_assets[$awardee_key];
  155.           $awardee_options[$awardee_key] = $asset->getName();
  156.         }
  157.         $form['awardee'] = [
  158.           '#type' => 'select',
  159.           '#title' => t('Awardee Organization'),
  160.           '#options' => $awardee_options,
  161.           '#required' => FALSE
  162.         ];
  163.  
  164.  
  165.         $form['create_organization'] = [
  166.             '#type' => 'button',
  167.             '#value' => $this->t('Create New Awardee Organization(s)'),
  168.             '#attributes' => array('onClick' => 'alert(1)'),
  169.         ];
  170.  
  171.         $form['#tree'] = TRUE; // TODO: figure out what this accomplishes
  172.        
  173.         $form['names_fieldset'] = [
  174.           '#prefix' => '<div id="names-fieldset-wrapper"',
  175.           '#suffix' => '</div>',
  176.         ];
  177.  
  178.         // Start: Load the list of existing contact types
  179.  
  180.         $contact_type_terms = \Drupal::entityTypeManager() -> getStorage('taxonomy_term') -> loadByProperties(
  181.             ['vid' => 'd_contact_type']
  182.         );
  183.  
  184.         $contact_type_options = [];
  185.         $contact_type_keys = array_keys($contact_type_terms);
  186.         foreach($contact_type_keys as $contact_type_key) {
  187.             $term = $contact_type_terms[$contact_type_key];
  188.             $contact_type_options[$contact_type_key] = $term -> getName();
  189.         }
  190.  
  191.         // End: Load the list of existing contact types
  192.  
  193.         $contact_type_form_element_settings = [
  194.             '#type' => 'select',
  195.             '#title' => $this->t('Contact Type'),
  196.             '#options' => $contact_type_options,
  197.             '#prefix' => '<div class="inline-components"',
  198.             '#suffix' => '</div>',
  199.         ];
  200.  
  201.         $contact_name_form_element_settings = [
  202.             '#type' => 'select',
  203.             '#title' => $this
  204.               ->t("Contact Name"),
  205.             '#options' => [
  206.               '' => $this
  207.                 ->t(' - Select - '),
  208.               'dacn1' => $this
  209.                 ->t('Dummy awardee contact name 1'),
  210.               'dacn2' => $this
  211.                 ->t('Dummy awardee contact name 2'),
  212.               'dacn3' => $this
  213.                 ->t('Dummy awardee contact name 3'),
  214.             ],
  215.             'attributes' => [
  216.                 'class' => 'something',
  217.             ],
  218.  
  219.             // TODO: Check if this is the best way
  220.             '#prefix' => ($num_lines > 1) ? '<div class="inline-components-short">' : '<div class="inline-components">',
  221.               '#suffix' => '</div>',
  222.         ];
  223.  
  224.         $contact_delete_button_form_element_settings = [
  225.             '#type' => 'submit',
  226.             '#attributes' => array('onClick' => 'alert(1)'),
  227.             '#value' => $this->t('Delete'),
  228.             '#name' => 'delete_button',
  229.             '#submit' => ['::removeCallback'],
  230.             '#ajax' => [
  231.               'callback' => '::addmoreCallback',
  232.               'wrapper' => 'names-fieldset-wrapper',
  233.             ],
  234.             "#limit_validation_errors" => array(),
  235.             '#prefix' => '<div class="remove-button-container">',
  236.             '#suffix' => '</div>',
  237.         ];
  238.  
  239.         // contact_delete_button_form_element_settings = []
  240.  
  241.         // This is temporary, need to go
  242.         for ($i = 0; $i < 10; $i++) {//num_indexes: get num of added contacts. (1->n)
  243.             print_r("Num indexes go brr");
  244.             if (in_array($i, $removed_fields)) {// Check if field was removed
  245.                 continue;
  246.             }
  247.  
  248.             $form['names_fieldset'][$i]['contact_name'] = $contact_name_form_element_settings;
  249.  
  250.             $form['names_fieldset'][$i]['contact_type'] = $contact_type_form_element_settings;
  251.  
  252.             $form['names_fieldset'][$i]['actions'] = $contact_delete_button_form_element_settings;
  253.  
  254.             //css space for a new line due to previous items' float left attr
  255.             $form['names_fieldset'][$i]['new_line_container'] = [
  256.                 '#markup' => '<div class="clear-space"></div>'
  257.             ];
  258.            
  259.         }
  260.  
  261.        
  262.        
  263.         $form['names_fieldset']['actions']['add_name'] = [
  264.             '#type' => 'submit',
  265.             '#button_type' => 'button',
  266.             '#name' => 'add_contact_button',
  267.             '#value' => t('Add Another Contact'),
  268.             '#submit' => ['::addOne'],
  269.             '#ajax' => [
  270.                 'callback' => '::addmoreCallback',
  271.                 'wrapper' => 'names-fieldset-wrapper',
  272.             ],
  273.             '#states' => [
  274.                 'visible' => [
  275.                     ":input[name='names_fieldset[0][contact_name]']" => ['!value' => ''],
  276.                     "and",
  277.                     ":input[name='names_fieldset[0][contact_type]']" => ['!value' => ''],
  278.                 ],
  279.             ],
  280.             "#limit_validation_errors" => array(),
  281.             '#prefix' => '<div id="addmore-button-container">',
  282.             '#suffix' => '</div>',
  283.         ];
  284.        
  285.         $this->buildProducerInformationSection($form, $form_state, $options);
  286.         $form_state->setCached(FALSE);
  287.  
  288.         $form['actions']['save'] = [
  289.             '#type' => 'submit',
  290.             '#value' => $this->t('Save'),
  291.         ];
  292.  
  293.         $form['actions']['cancel'] = [
  294.             '#type' => 'submit',
  295.             '#value' => $this->t('Cancel'),
  296.         ];
  297.  
  298.         return $form;
  299.     }
  300.  
  301.   /**
  302.    * {@inheritdoc}
  303.    */
  304.   public function validateForm(array &$form, FormStateInterface $form_state){
  305.     return;
  306.   }
  307.  
  308.   /**
  309.    * {@inheritdoc}
  310.    */
  311.   public function submitForm(array &$form, FormStateInterface $form_state) {
  312.  
  313.     $this->messenger()->addStatus($this->t('Form submitted!'));
  314.  
  315.     if($form_state->getValues()['actions']['#value'] == t('Save')){
  316.         $this
  317.         ->messenger()
  318.         ->addStatus($this
  319.         ->t('Form submitted for project @project_name', [
  320.             '@project_name' => $form['project_name']['#value'],
  321.         ])
  322.         );
  323.     }
  324.   }
  325.  
  326.   /**
  327.    * {@inheritdoc}
  328.    */
  329.   public function getFormId() {
  330.     return 'project_create_form';
  331.   }
  332.  
  333.   public function addmoreCallback(array &$form, FormStateInterface $form_state) {
  334.     return $form['names_fieldset'];
  335.   }
  336.  
  337.   public function addOne(array &$form, FormStateInterface &$form_state) {
  338.     print_r("BRRRR");
  339.     $this->messenger()->addStatus('BRRRR');
  340.     $num_indexes = $form_state->get('num_indexes');
  341.     $num_lines = $form_state->get('num_lines');
  342.     $form_state->set('num_indexes', $num_indexes + 1);
  343.     $form_state->set('num_lines', $num_lines + 1);
  344.     $form_state->setRebuild();
  345.   }
  346.  
  347.  
  348.  
  349.   public function removeContactFromList(array &$form, FormStateInterface &$form_state){
  350.       $trigger = $form_state->getTriggeringElement();
  351.   }
  352.  
  353.  
  354.  
  355.   public function removeCallback(array &$form, FormStateInterface &$form_state) {
  356.     print_r("Removecallbackgobrrr");
  357.     $trigger = $form_state->getTriggeringElement();
  358.     $num_line = $form_state->get('num_lines');
  359.     $indexToRemove = $trigger['#name'];
  360.  
  361.     // Remove the fieldset from $form (the easy way)
  362.     unset($form['names_fieldset'][$indexToRemove]);
  363.  
  364.     // Keep track of removed fields so we can add new fields at the bottom
  365.     // Without this they would be added where a value was removed
  366.     $removed_fields = $form_state->get('removed_fields');
  367.     $removed_fields[] = $indexToRemove;
  368.     $form_state->set('removed_fields', $removed_fields);
  369.     $form_state->set('num_lines', $num_line - 1);
  370.  
  371.     // Rebuild form_state
  372.     $form_state->setRebuild();
  373.   }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment