You are here

function bundle_inherit_perform in Bundle Inherit 7

Perform necesary inherit operations.

2 calls to bundle_inherit_perform()
BundleInherit::inheritPerform in ./bundle_inherit.test
bundle_inherit_attach_inherit_form_submit in ./bundle_inherit.module
Should be executed when entity creation form is submiting.

File

./bundle_inherit.module, line 10
Bundle Inherit module.

Code

function bundle_inherit_perform($entity_type, $bundle, $bundle_parent, $strict = TRUE) {

  // Get fields from parent bundle.
  $instances = field_info_instances($entity_type, $bundle_parent);
  foreach ($instances as $instance) {
    $new_instance = $instance;
    $new_instance['bundle'] = $bundle;
    if ($strict) {
      $new_instance['locked'] = TRUE;
    }
    $new_instance = field_create_instance($new_instance);
    $query = db_select('field_config_instance', 'fci');
    $query
      ->addField('fci', 'id');
    $query
      ->condition('fci.bundle', $bundle);
    $new_instance['id'] = $query
      ->execute()
      ->fetchField();
  }

  // Check if we perform strict inheritance.
  if ($strict) {
    $exists = db_query('SELECT 1 FROM {bundle_inherit} WHERE entity_type = :entity_type AND bundle = :bundle', array(
      ':entity_type' => $entity_type,
      ':bundle' => $bundle,
    ))
      ->fetchField();
    if ($exists) {
      db_update('bundle_inherit')
        ->fields(array(
        'bundle_parent' => $bundle_parent,
      ))
        ->condition('entity_type', $entity_type)
        ->condition('bundle', $bundle)
        ->execute();
    }
    else {
      db_insert('bundle_inherit')
        ->fields(array(
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'bundle_parent' => $bundle_parent,
      ))
        ->execute();
    }
    watchdog('bundle_inherit', 'The %bundle bundle of the entity %type was STRICTLY inherited from %parent_bundle bundle.', array(
      '%bundle' => $bundle,
      '%bundle_parent' => $bundle_parent,
      '%type' => $entity_type,
    ));
    drupal_static_reset('bundle_inherit_bundle_get_children');
  }
  else {
    watchdog('bundle_inherit', 'The %bundle bundle of the entity %type was SOFTLY inherited from %parent_bundle bundle.', array(
      '%bundle' => $bundle,
      '%bundle_parent' => $bundle_parent,
      '%type' => $entity_type,
    ));
  }

  // Allow third party modules to implement there own inherit logic.
  module_invoke_all('bundle_inherit_perform', $entity_type, $bundle, $bundle_parent, $strict = TRUE);
}