You are here

function machine_name_field_widget_form in Machine name 7

Same name and namespace in other branches
  1. 7.2 machine_name.module \machine_name_field_widget_form()

Implements hook_field_widget_form().

File

./machine_name.module, line 66
Defines a machine name field type, relying on Form API's #type 'machine_name'.

Code

function machine_name_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Include #title, #description, #required, and other field and instance
  // properties provided by Field Form API.
  $element['value'] = (array) $element;
  $element['value'] += array(
    '#type' => 'machine_name',
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
    '#machine_name' => array(
      'exists' => 'machine_name_value_exists',
      'replace_pattern' => $field['settings']['replace_pattern'],
      'replace' => $field['settings']['replace'],
    ),
    '#maxlength' => $field['settings']['max_length'],
  );

  // Machine names cannot be edited if there is a value. The only exception is
  // when previewing a new entity or field marked as "editable".
  $disabled = TRUE;
  $editable = !empty($instance['widget']['settings']['editable']);
  if (!isset($items[$delta]) || machine_name_field_is_empty($items[$delta], $field) || $editable) {
    $disabled = FALSE;
  }
  elseif (!empty($form['#node_edit_form']) && !isset($form['nid']['#value'])) {
    $disabled = FALSE;
  }
  $element['value']['#disabled'] = $disabled;
  return $element;
}