Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanLaufer/18d3ad3bc1f2bd73ad7802949dd8f50c to your computer and use it in GitHub Desktop.
Save DanLaufer/18d3ad3bc1f2bd73ad7802949dd8f50c to your computer and use it in GitHub Desktop.
Drupal 8 - Module create content type and fields on install
<?php
/**
* Implements hook_install().
*/
function gamecontenttype_install() {
node_types_rebuild();
$types = node_type_get_types();
foreach(_gamecontenttype_fields() as $field) {
field_create_field($field);
}
foreach(_gamecontenttype_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'game';
print_r($fieldinstance);
field_create_instance($fieldinstance);
}
}
/**
* Implements hook_uninstall().
*/
function gamecontenttype_uninstall() {
$ournewtype = 'game';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
delete_game_fields();
node_type_delete($ournewtype);
field_purge_batch(500);
}
function delete_game_fields() {
foreach (array_keys(_gamecontenttype_fields()) as $field) {
field_delete_field($field);
}
$instances = field_info_instances('node', 'game');
foreach ($instances as $instance_name => $fieldinstance) {
field_delete_instance($fieldinstance);
}
}
function _gamecontenttype_fields() {
$t = get_t();
return array(
'game_player_1' => array(
'field_name' => 'game_player_1',
'label' => $t('Player one'),
'cardinality' => 1,
'type' => 'user_reference',
'entity_types' => array('node'),
'settings' => array(
'referenceable_status' => array(
0 => 0,
1 => '1',
),
)
),
'game_player_2' => array(
'field_name' => 'game_player_2',
'label' => $t('Player two'),
'cardinality' => 1,
'type' => 'user_reference',
'entity_types' => array('node'),
'settings' => array(
'referenceable_status' => array(
0 => 0,
1 => '1',
),
)
),
'game_winner' => array(
'field_name' => 'game_winner',
'label' => $t('Who is the winner?'),
'cardinality' => 1,
'type' => 'user_reference',
'entity_types' => array('node'),
'settings' => array(
'referenceable_status' => array(
0 => 0,
1 => '1',
),
)
),
);
}
function _gamecontenttype_instances() {
$t = get_t();
return array(
'game_player_1' => array(
'field_name' => 'game_player_1',
'entity_type' => array('node'),
'bundle' => 'game',
'label' => $t('Player One'),
'required' => TRUE,
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(
'apply_chosen' => '',
),
'type' => 'options_select',
'weight' => '40',
),
'display' => array(),
),
'game_player_2' => array(
'field_name' => 'game_player_2',
'entity_type' => array('node'),
'bundle' => 'game',
'label' => $t('Player Two'),
'required' => TRUE,
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(
'apply_chosen' => '',
),
'type' => 'options_select',
'weight' => '40',
),
'display' => array(),
),
'game_winner' => array(
'field_name' => 'game_winner',
'entity_type' => array('node'),
'bundle' => 'game',
'label' => $t('Game Winner'),
'required' => TRUE,
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(
'apply_chosen' => '',
),
'type' => 'options_select',
'weight' => '41',
),
'display' => array(),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment