2

I would like to add multiple existing fields to a content type using drush. Right now I'm using the following to create the fields:

drush field-create [content-type] [field-machine-name],[field-type]

Is there any way to do something like the following instead?

drush field-create [content-type] [field-label],[field-machine-name],[field-type]
2

I am afraid you need to edit drush files to achieve it.. Currently label support is not there if you see below code in contributions/drush/commands/core/field.drush.inc

function drush_field_create($bundle) {
  $entity_type = drush_get_option('entity_type', 'node');

  $args = func_get_args();
  array_shift($args);
  if (empty($args)) {
    // Just one item in this array for now.
    $args[] = drush_field_create_wizard();
  }

  // Iterate over each field spec.
  foreach ($args as $string) {
    list($name, $type, $widget) = explode(',', $string);
    $info = field_info_field($name);
    if (empty($info)) {
      // Field does not exist already. Create it.
      $field = array(
        'field_name' => $name, 
        'type' => $type,
      );
      drush_op('field_create_field', $field);
    }

    // Create the instance.
    $instance = array(
      'field_name' => $name, 
      'entity_type' => $entity_type, 
      'bundle' => $bundle,
    );
    if ($widget) {
      $instance['widget'] = array('type' => $widget);
    }
    drush_op('field_create_instance', $instance);

    $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
  }
  drush_print(implode(' ', $urls));
}
?>

As you see above only field_name and type is taken from args.. To create label also you need to change code to below

function drush_field_create($bundle) {
  $entity_type = drush_get_option('entity_type', 'node');

  $args = func_get_args();
  array_shift($args);
  if (empty($args)) {
    // Just one item in this array for now.
    $args[] = drush_field_create_wizard();
  }

  // Iterate over each field spec.
  foreach ($args as $string) {
    list($name, $label, $type, $widget) = explode(',', $string);
    $info = field_info_field($name);
    if (empty($info)) {
      // Field does not exist already. Create it.
      $field = array(
        'field_name' => $name, 
        'type' => $type,
        'label' => $label,
      );
      drush_op('field_create_field', $field);
    }

    // Create the instance.
    $instance = array(
      'field_name' => $name, 
      'entity_type' => $entity_type, 
      'bundle' => $bundle,
    );
    if ($widget) {
      $instance['widget'] = array('type' => $widget);
    }
    drush_op('field_create_instance', $instance);

    $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
  }
  drush_print(implode(' ', $urls));
}
?>

and use command like

drush field-create [content-type] [field-machine-name],[field-label],[field-type],[field-widget-type]
  • Two things, where is the file 'contributions/drush/commands/core/field.drush.inc' and does this work for labels that have spaces, e.g. 'First Name'? – tsega Jun 7 '14 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.