Drupal → Как при установке модуля создать словарь с полем

20.12.2013

Пример создания словаря cities с полем field_custom_text и одним термином при установке модуля:

// modulename.install

/**
 * Implements hook_install().
 */
function modulename_install() {
  if (!taxonomy_vocabulary_machine_name_load('cities')) {
    // Create vocabulary
    $vocabulary = (object)array(
      'name' => 'Cities',
      'machine_name' => 'cities',
      'module' => 'modulename',
    );
    taxonomy_vocabulary_save($vocabulary);

    // Create field "field_custom_text"
    field_create_field(array(
      'field_name' => 'field_custom_text',
      'type' => 'text',
      'cardinality' => 1,
    ));
    field_create_instance(array(
      'field_name' => 'field_custom_text',
      'entity_type' => 'taxonomy_term',
      'bundle' => 'cities',
      'label' => t('Custom text'),
      'widget' => array(
        'type' => 'text_textfield',
      ),
    ));

    // Create term
    $term = (object)array(
      'vid' => $vocabulary->vid,
      'name' => 'My city',
      'field_custom_text' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'some text',
          ),
        ),
      ),
    );
    taxonomy_term_save($term);
  }
}

/**
 * Implements hook_uninstall().
 */
function modulename_uninstall() {
  // Delete vocabulary
  if ($vocabulary = taxonomy_vocabulary_machine_name_load('cities')) {
    taxonomy_vocabulary_delete($vocabulary->vid);
  }
}
Написанное актуально для
Drupal 7
Похожие записи

Добавить комментарий