You are here

function oa_core_create_term in Open Atrium Core 7.2

Helper function to create a new Section or Space Type taxonomy term

Parameters

string $vocab: Machine name of vocabulary to add term to

string $name: Human readable label of the term

array $params: expected to have fields for: 'taxonomy' - vocabulary machine name. 'name' - The human readable label of the term. 'description' - The human readable description of the term. 'node_options' - An array of node types which should be allowed within the section. 'layout' - The panelizer layout key to use for this section. 'icon' - The icon class to use in the sitemap.

bool $update: Whether an existing term is allowed to be updated.

1 call to oa_core_create_term()
oa_core_create_default_terms in ./oa_core.install
Create the default Space and Section taxonomy terms

File

includes/oa_core.util.inc, line 1364
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_create_term($vocab_name, $name, $params, $update = TRUE) {
  $vocab = oa_core_taxonomy_vocabulary($vocab_name);
  $term = NULL;

  // This function can get called from install hooks.
  // Make sure the Taxonomy is available.
  if (!$vocab) {
    features_revert(array(
      'oa_core' => array(
        'taxonomy',
      ),
      'oa_sections' => array(
        'taxonomy',
      ),
    ));
    drupal_static('taxonomy_vocabulary_get_names');
    $vocab = oa_core_taxonomy_vocabulary($vocab_name);
  }
  if (!empty($vocab)) {
    $conditions = array(
      'name' => trim($name),
      'vid' => $vocab->vid,
    );
    $term = current(entity_load('taxonomy_term', array(), $conditions));
    if (!$term || $update) {

      // Make sure the fields are available.
      $field_info = field_info_instances('taxonomy_term', $vocab_name);
      $revert = array();
      if (empty($field_info['field_oa_node_types'])) {
        $revert['oa_buttons'] = array(
          'field_base',
          'field_instance',
        );
      }
      if (empty($field_info['field_oa_section_layout'])) {
        $revert['oa_core'] = array(
          'field_base',
          'field_instance',
        );
      }
      if (empty($field_info['field_oa_icon_class'])) {
        $revert['oa_sections'] = array(
          'field_base',
          'field_instance',
        );
      }
      if (!empty($revert)) {
        features_revert($revert);
        field_info_cache_clear();
      }
      $description = $params['description'];
      $node_options = $params['node_options'];
      $layout = $params['layout'];
      $icon = !empty($params['icon']) ? $params['icon'] : '';
      if ($term && $update) {

        // Update existing term.
        if (isset($term->field_oa_icon_class)) {
          $term->field_oa_icon_class[LANGUAGE_NONE][0]['value'] = $icon;
        }
        $term->field_oa_section_layout[LANGUAGE_NONE][0]['value'] = $layout;
        $term->field_oa_node_types[LANGUAGE_NONE] = array();
      }
      else {

        // Create new term.
        $term = (object) array(
          'vid' => $vocab->vid,
          'name' => $name,
          'description' => $description,
          'format' => 'panopoly_wysiwyg_text',
          'field_oa_icon_class' => array(
            LANGUAGE_NONE => array(
              array(
                'value' => $icon,
              ),
            ),
          ),
          'field_oa_section_layout' => array(
            LANGUAGE_NONE => array(
              array(
                'value' => $layout,
              ),
            ),
          ),
          'field_oa_node_types' => array(
            LANGUAGE_NONE => array(),
          ),
          'path' => array(
            'alias' => '',
            'pathauto' => '',
          ),
          'oa_button' => TRUE,
        );
      }
      if (!empty($node_options)) {
        foreach ($node_options as $type) {
          $term->field_oa_node_types[LANGUAGE_NONE][] = array(
            'value' => $type,
          );
        }
      }
      taxonomy_term_save($term);
    }
  }
  return $term;
}