You are here

function organigrams_form_alter in Organigrams 8.2

Same name and namespace in other branches
  1. 8 organigrams.module \organigrams_form_alter()

Implements hook_form_alter().

File

./organigrams.module, line 42
Extends Taxonomy to create organigrams.

Code

function organigrams_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Alter the vocabulary form.
  if ($form_id == 'taxonomy_vocabulary_form') {

    // Get the vocabulary.
    $vocabulary = $form_state
      ->getFormObject()
      ->getEntity();

    // Get its organigrams settings if they exist.
    $organigram = $vocabulary
      ->getThirdPartySettings('organigrams');

    // Check if the current page is a vocabulary add form and if there are
    // organigrams settings.
    if (\Drupal::routeMatch()
      ->getRouteName() == 'entity.vocabulary.add_organigram_form' || !empty($organigram)) {

      // Add our own submit function to the submit button.
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = 'organigrams_vocabulary_form_submit';
        }
      }

      // Change the form title when adding and editing.
      if (!empty($organigram)) {
        $form['#title'] = t('Edit organigram vocabulary');
      }
      else {
        $form['#title'] = t('Add organigram vocabulary');
      }
    }
  }
  elseif ($form_id == 'taxonomy_overview_terms') {

    // Get the vocabulary and check if it's and organigram.
    $build_info = $form_state
      ->getBuildInfo('args');
    $organigram = $build_info['args'][0]
      ->getThirdPartySettings('organigrams');

    // Check if there are terms.
    if (!empty($organigram) && !empty($form['terms'])) {

      // Iterate through them.
      foreach ($form['terms'] as $key => $value) {

        // Pick out the terms by checking on tid in the key.
        if (substr($key, 0, 3) == 'tid') {

          // Get the position value.
          $position = $value['#term']
            ->get('field_o_position')
            ->first()
            ->getValue();

          // If the positions is s, prepend 'Staff' to the term title.
          if ($position['value'] == 's') {
            $form['terms'][$key]['term']['#title'] = 'Staff: ' . $form['terms'][$key]['term']['#title'];
          }
        }
      }
    }
  }
}