function forum_enable

Implements hook_enable().

File

drupal/core/modules/forum/forum.install, line 24
Install, update, and uninstall functions for the Forum module.

Code

function forum_enable() {

  // Create the forum vocabulary if it does not exist.
  // @todo Change Forum module so forum.settings can contain the vocabulary's
  //   machine name.
  $config = config('forum.settings');

  // If the module was disabled only, the current config may contain a valid
  // vocabulary ID already.
  $vocabulary = taxonomy_vocabulary_load($config
    ->get('vocabulary'));
  if (!$vocabulary) {

    // If the module was installed and uninstalled previously, the vocabulary
    // with machine name 'forums' might still exist.
    $vocabulary = taxonomy_vocabulary_load('forums');
    if (!$vocabulary) {

      // Otherwise, installing from scratch; create the vocabulary.
      $vocabulary = entity_create('taxonomy_vocabulary', array(
        'name' => t('Forums'),
        'vid' => 'forums',
        'langcode' => language_default()->langcode,
        'description' => t('Forum navigation vocabulary'),
        'hierarchy' => 1,
        'module' => 'forum',
        'weight' => -10,
      ));
      $vocabulary
        ->save();
    }
    $config
      ->set('vocabulary', $vocabulary
      ->id())
      ->save();
  }

  // Create the 'taxonomy_forums' field if it doesn't already exist. If forum
  // is being enabled at the same time as taxonomy after both modules have been
  // enabled, the field might exist but still be marked inactive.
  if (!field_read_field('taxonomy_forums', array(
    'include_inactive' => TRUE,
  ))) {
    $field = array(
      'field_name' => 'taxonomy_forums',
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $vocabulary
              ->id(),
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    // Create a default forum so forum posts can be created.
    $term = entity_create('taxonomy_term', array(
      'name' => t('General discussion'),
      'langcode' => language_default()->langcode,
      'description' => '',
      'parent' => array(
        0,
      ),
      'vid' => $vocabulary
        ->id(),
    ));
    $term
      ->save();

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'taxonomy_forums',
      'entity_type' => 'node',
      'label' => $vocabulary->name,
      'bundle' => 'forum',
      'required' => TRUE,
    );
    field_create_instance($instance);

    // Assign form display settings for the 'default' form mode.
    entity_get_form_display('node', 'forum', 'default')
      ->setComponent('taxonomy_forums', array(
      'type' => 'options_select',
    ))
      ->save();

    // Assign display settings for the 'default' and 'teaser' view modes.
    entity_get_display('node', 'forum', 'default')
      ->setComponent('taxonomy_forums', array(
      'type' => 'taxonomy_term_reference_link',
      'weight' => 10,
    ))
      ->save();
    entity_get_display('node', 'forum', 'teaser')
      ->setComponent('taxonomy_forums', array(
      'type' => 'taxonomy_term_reference_link',
      'weight' => 10,
    ))
      ->save();
  }

  // Ensure the forum node type is available.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types['forum']);
}