Vous pouvez utiliser le code customcompanymodule donné ci-dessous pour créer par programme un type de contenu avec ses différents champs.
Vous pouvez ajouter ce code dans un fichier .install de votre module personnalisé. Il ajoutera par programme un type de contenu appelé "société" et ses différents types de champs (texte, numérique, date (remarque: vous devrez installer le module Date car le champ Date n'est pas fourni par défaut), image, liste).
J'ai également ajouté le code de désinstallation qui supprimera le type de contenu "société", ainsi que tous ses champs et données, lors de la désinstallation de votre module "customcompanymodule".
Vous pouvez modifier / supprimer ces champs selon vos besoins:
function customcompanymodule_install() {
$t = get_t();
node_types_rebuild();
$company = array(
'type' => 'company',
'name' => $t('Company'),
'base' => 'node_content',
'module' => 'node',
'description' => $t('Content type to handle companys.'),
'body_label' => $t('Company Description'),
'title_label' => $t('Company Title'),
'promote' => 0,
'status' => 1,
'comment' => 0,
);
$content_type = node_type_set_defaults($company);
node_type_save($content_type);
foreach (_company_installed_fields() as $field) {
field_create_field($field);
}
foreach (_company_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = 'company';
field_create_instance($instance);
}
$weight = db_query("SELECT weight FROM {system} WHERE name = :name", array(':name' => 'categories'))->fetchField();
db_update('system')->fields(array(
'weight' => $weight + 1,
))
->condition('name', 'company')
->execute();
}
function _company_installed_fields() {
$t = get_t();
$fields = array(
'company_startdate' => array(
'field_name' => 'company_startdate',
'label' => $t('Company Start Date'),
'cardinality' => 1,
'type' => 'datetime',
'module' => 'date',
'settings' => array(
'granularity' => array(
'month' => 'month',
'day' => 'day',
'hour' => 'hour',
'minute' => 'minute',
'year' => 'year',
'second' => 0,
),
'tz_handling' => 'site',
'timezone_db' => 'UTC',
'cache_enabled' => 0,
'cache_count' => '4',
'todate' => 'required',
),
),
'company_totalwinners' => array(
'field_name' => 'company_totalwinners',
'label' => $t('Maximum Company Winners'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_minwinner' => array(
'field_name' => 'company_minwinner',
'label' => $t('Minimum Entries for Company to Activate'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_totalentries' => array(
'field_name' => 'company_totalentries',
'label' => $t('Company Total Entries'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_points' => array(
'field_name' => 'company_points',
'label' => $t('Company Points'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_image' => array(
'field_name' => 'company_image',
'label' => $t('Image'),
'cardinality' => 1,
'type' => 'image',
'settings' => array(
'default_image' => 0,
'uri_scheme' => 'public',
),
),
'company_description' => array(
'field_name' => 'company_description',
'label' => $t('Company Description'),
'cardinality' => 1,
'type' => 'text',
'module' => 'text',
'length' => '255'
),
'company_winner' => array(
'field_name' => 'company_winner',
'label' => $t('Company Description'),
'cardinality' => 1,
'type' => 'text',
'module' => 'text',
'length' => '255'
),
'field_autowinnerselection' => array(
'field_name' => 'field_autowinnerselection',
'label' => $t('Auto Company Winner Selection'),
'type' => 'list_boolean',
'module' => 'list',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
'deleted' => '0'
),
);
return $fields;
}
function _company_installed_instances() {
$t = get_t();
$instances = array(
'company_startdate' => array(
'field_name' => 'company_startdate',
'label' => $t('Company Lifespan'),
'cardinality' => 1,
'widget' => array(
'type' => 'date_popup',
'module' => 'date',
'settings' => array(
'input_format' => 'm/d/Y - H:i:s',
'input_format_custom' => '',
'year_range' => '-3:+3',
'increment' => '15',
'label_position' => 'above',
'text_parts' => array(),
),
),
),
'company_totalwinners' => array(
'field_name' => 'company_totalwinners',
'label' => $t('Maximum Company Winners'),
'cardinality' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_minwinner' => array(
'field_name' => 'company_minwinner',
'label' => $t('Minimum Number of Entries for Company to Activate'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_totalentries' => array(
'field_name' => 'company_totalentries',
'label' => $t('Company Total Entries'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_points' => array(
'field_name' => 'company_points',
'label' => $t('Company Points'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_image' => array(
'field_name' => 'company_image',
'label' => $t('Image'),
'cardinality' => 1,
'required' => 1,
'type' => 'company_image',
'settings' => array(
'max_filesize' => '',
'max_resolution' => '213x140',
'min_resolution' => '213x140',
'alt_field' => 1,
'default_image' => 0
),
'widget' => array(
'settings' => array(
'preview_image_style' => 'thumbnail',
'progress_indicator' => 'throbber',
),
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array('image_style' => 'medium', 'image_link' => ''),
'weight' => -1,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array('image_style' => 'thumbnail', 'image_link' => 'content'),
'weight' => -1,
),
),
),
'company_description' => array(
'field_name' => 'company_description',
'label' => $t('Company Description'),
'cardinality' => 1,
'widget' => array(
'weight' => '-3',
'type' => 'text_textfield',
'module' => 'text',
'active' => 1,
'settings' => array(
'size' => '1000',
),
),
),
'company_winner' => array(
'field_name' => 'company_winner',
'label' => $t('Company Winner'),
'cardinality' => 1,
'widget' => array(
'weight' => '-3',
'type' => 'text_textfield',
'module' => 'text',
'active' => 1,
'settings' => array(
'size' => '60',
),
),
),
'field_autowinnerselection' => array(
'field_name' => 'field_autowinnerselection',
'required' => 1,
'label' => $t('Auto Company Winner Selection'),
'widget' => array(
'weight' => '-3',
'type' => 'options_buttons',
'module' => 'options',
'active' => 1,
'settings' => array(),
),
),
);
return $instances;
}
function customcompanymodule_uninstall() {
$content_types = array(
'name1' => 'company',
);
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type1';
$result = db_query($sql, array(':type1' => $content_types['name1']));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
node_delete_multiple($nids);
node_type_delete($content_types['name1']);
field_purge_batch(1000);
}
node_type_set_defaults()
etnode_type_save()
, ainsi que, bien sûr,hook_install()
.