You are here

function _cer_rebuild_preset in Corresponding Entity References 7.3

Rebuilds a legacy CER or CNR preset as a CerPreset entity.

2 string references to '_cer_rebuild_preset'
cer_update_7005 in ./cer.install
Creates the {cer_preset} table to store CerPreset entities, and rebuilds legacy presets.
_cer_hijack_cnr in ./cer.install
Converts all CNR presets to CER presets, then disables CNR.

File

./cer.install, line 438
Install file providing corresponding entity reference schema.

Code

function _cer_rebuild_preset($old) {
  if (isset($old->entity_types_content_fields)) {
    $keys = explode('*', $old->entity_types_content_fields);
    $old->a = implode(':', array_slice($keys, 0, 3));
    $old->b = implode(':', array_slice($keys, 3));
  }
  if (!isset($old->bidirectional)) {
    $old->bidirectional = TRUE;
  }
  if (!isset($old->weight)) {
    $old->weight = 0;
  }

  // If the 'enabled' flag doesn't exist, we can presume that this preset
  // was exported in code generated by CTools, which means that it should
  // have a 'disabled' flag, which we can invert to get the status.
  if (!isset($old->enabled)) {
    $old->enabled = !$old->disabled;
  }

  // Validate both field chains by trying to unpack them. If any field in
  // either chain isn't exposed by hook_cer_fields(), an exception will be
  // thrown and we can bail out with an error message.
  try {
    CerFieldChain::unpack($old->a);
    CerFieldChain::unpack($old->b);
  } catch (Exception $e) {
    $variables = array(
      '%key' => "{$old->a}*{$old->b}",
    );
    return drupal_set_message(t('Could not rebuild preset %key because it refers to invalid fields.', $variables), 'error');
  }
  $preset = new CerPreset();
  $preset->wrapper->cer_left
    ->set($old->a);
  $preset->wrapper->cer_right
    ->set($old->b);
  $preset->wrapper->cer_enabled
    ->set($old->enabled);
  $preset->wrapper->cer_bidirectional
    ->set($old->bidirectional);
  $preset->wrapper->cer_weight
    ->set($old->weight);
  $preset
    ->save();
}