Skip to content

Instantly share code, notes, and snippets.

@zaporylie
Last active January 20, 2016 11:22
Show Gist options
  • Save zaporylie/444253c592f139a5c0bd to your computer and use it in GitHub Desktop.
Save zaporylie/444253c592f139a5c0bd to your computer and use it in GitHub Desktop.
Exporting/importing fields/groups
<?php
/**
* Get field/instance
*/
function get_field_install($field_name) {
$field = field_info_field($field_name);
unset($field['columns']);
unset($field['id']);
unset($field['indexes']);
unset($field['foreign keys']);
unset($field['storage']);
unset($field['translatable']);
unset($field['entity_types']);
unset($field['bundles']);
foreach ($field as $k => $f) {
if (is_bool($f)) {
$field[$k] = boolval($f);
}
elseif (is_numeric($f)) {
$field[$k] = intval($f);
}
}
return $field;
}
function get_instance_install($entity_type, $field_name, $bundle) {
$field = field_info_instance($entity_type, $field_name, $bundle);
unset($field['field_id']);
unset($field['id']);
foreach ($field as $k => $f) {
if (is_bool($f)) {
$field[$k] = boolval($f);
}
elseif (is_numeric($f)) {
$field[$k] = intval($f);
}
elseif (in_array($k, array('label', 'description'))) {
$field[$k] = '$t(\'' . $f . '\')';
}
}
return $field;
}
foreach (field_info_field_map() as $field_name => $field) {
// Get field info.
dvm(get_field_install($field_name), $field_name);
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
// Get instance info.
dvm(get_instance_install($entity_type, $field_name, $bundle), "$entity_type => $field_name <= $bundle");
}
}
}
<?php
/**
* Load/Save fieldgroups
*/
$groups = field_group_read_groups(array('name' => 'node', 'bundle' => 'apartment'));
$save_group = $groups['node']['apartment']['form']['group_name'];
// Add existing fields to group (by name).
$save_group->children[] = 'field_name';
field_group_group_save($save_group);
// Creates new group.
$new_group = prepare_form_group('test', 'Test', 0, array('title'));
field_group_group_save($new_group);
function prepare_form_group($name, $label, $weight = 0, $children = array(), $bundle = 'article', $entity_type = 'node') {
$t = get_t();
return (object) array(
'id' => NULL,
'identifier' => 'group_' . $name . '|' . $entity_type . '|' . $bundle . '|form',
'group_name' => 'group_' . $name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'mode' => 'form',
'parent_name' => '',
'table' => 'field_group',
'type' => 'Normal',
'disabled' => FALSE,
'label' => $t($label),
'weight' => $weight,
'children' => $children,
'format_type' => 'tab',
'format_settings' => array(
'formatter' => 'closed',
'instance_settings' => array(
'description' => '',
'classes' => 'group-' . $name . ' field-group-tab',
'required_fields' => 1,
),
),
);
}
foreach (field_group_read_groups() as $entity_type => $rest) {
foreach ($rest as $bundle => $rest) {
foreach ($rest as $form => $rest) {
foreach ($rest as $group_name => $group) {
dvm($group, str_replace('group_', '', $group_name));
}
}
}
}
<?php
$permissions = array(); // From user_role_permissions();
$all_permissions = array_keys(module_invoke_all('permission'));
foreach ($permissions as $role => $perm) {
// Revoke permissions for the role.
db_delete('role_permission')->condition('rid', $role)->execute();
// Clear the user access cache.
drupal_static_reset('user_access');
drupal_static_reset('user_role_permissions');
// Grant access to exising permissions only.
foreach ($perm as $p => $s) {
if (in_array($p, $all_permissions)) {
user_role_grant_permissions($role, array($p));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment