You are here

function organigrams_vocabulary_settings_form in Organigrams 8

Helper function adding custom fields to the vocabulary add form.

Parameters

array $form: Contains the vocabulary add form.

array $organigram_settings: Contains the organigrams settings.

1 call to organigrams_vocabulary_settings_form()
organigrams_form_alter in ./organigrams.module
Implements hook_form_alter().

File

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

Code

function organigrams_vocabulary_settings_form(array &$form, array $organigram_settings) {

  // Set the config path of the vocabulary fields.
  $config_path = drupal_get_path('module', 'organigrams') . '/config/vocabulary_fields';

  // Get the field config files.
  $files = \Drupal::service('file_system')
    ->scanDirectory($config_path, '/^.*\\.yml$/i', [
    'key' => 'filename',
  ]);

  // Check if there are any files.
  if (!empty($files)) {

    // Iterate through the files.
    foreach ($files as $file) {

      // Get the file content which is yaml.
      $yaml = file_get_contents($file->uri);

      // Decode the yaml.
      $field_config = Yaml::decode($yaml);

      // Check if a field group is set and if so, add it to the form.
      if (!empty($field_config['field_group']) && !isset($form[$field_config['field_group']])) {
        $form[$field_config['field_group']] = [
          '#type' => 'fieldset',
          '#title' => t($field_config['field_group']),
          '#weight' => $field_config['field_group_weight'],
        ];
      }

      // Add the field to the field group.
      $form[$field_config['field_group']][$field_config['field_name']] = [
        '#type' => $field_config['type'],
        '#title' => t($field_config['label']),
        '#description' => t($field_config['description']),
        '#weight' => $field_config['weight'],
      ];

      // Check if it is required.
      if (isset($field_config['required'])) {
        $form[$field_config['field_group']][$field_config['field_name']]['#required'] = $field_config['required'];
      }

      // Check if there are options in case it's a select element.
      if (isset($field_config['options'])) {
        $form[$field_config['field_group']][$field_config['field_name']]['#options'] = $field_config['options'];
      }

      // Set a default value.
      if (isset($organigram_settings[$field_config['field_name']])) {
        $form[$field_config['field_group']][$field_config['field_name']]['#default_value'] = $organigram_settings[$field_config['field_name']];
      }
      elseif (isset($field_config['default'])) {
        $form[$field_config['field_group']][$field_config['field_name']]['#default_value'] = $field_config['default'];
      }
    }
  }
}