You are here

function custom_breadcrumbs_features_generate_missing_machine_names in Custom Breadcrumbs Features 7.2

Generates machine names for crumbs that do not have one.

Note: we assume users do not store hundreds of breadcrumb configs. Otherwise we should probably use the batch api.

Parameters

$table: Table storing crumbs.

Return value

Updated crumbs keyed by machine name.

2 calls to custom_breadcrumbs_features_generate_missing_machine_names()
custom_breadcrumbs_features_enable in ./custom_breadcrumbs_features.install
Implements hook_enable().
custom_breadcrumbs_features_modules_enabled in ./custom_breadcrumbs_features.install
Implements hook_modules_enabled().

File

./custom_breadcrumbs_features.install, line 140
Install file for custom_breadcrumbs_features.

Code

function custom_breadcrumbs_features_generate_missing_machine_names($table) {
  $transaction = db_transaction();

  // Get crumbs that miss a machine name.
  $crumbs = db_select($table, 'cb')
    ->fields('cb')
    ->isNull('machine_name')
    ->execute();

  // Generate machine names, if needed.
  if ($crumbs
    ->rowCount()) {

    // Get max length of machine_name field.
    $schema = drupal_get_schema($table);
    $maxlength = $schema['fields']['machine_name']['length'];
    foreach ($crumbs as $crumb) {

      // Generate unique machine name.
      $i = 0;
      do {
        $machine_name = custom_breadcrumbs_features_transliterate($crumb->name, $maxlength) . ($i ? "_{$i}" : '');
        $i++;
      } while (custom_breadcrumbs_features_generic_load($machine_name, $table));

      // Update crumb in DB.
      $crumb->machine_name = $machine_name;
      drupal_write_record($table, $crumb, array(
        'bid',
      ));
      drupal_set_message(t('Generated machine name %machine for @type %name.', array(
        '%machine' => $machine_name,
        '@type' => $table,
        '%name' => $crumb->name,
      )));
    }
  }
}