You are here

function _field_patterns_prepare_instance in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_components/components/field.inc \_field_patterns_prepare_instance()
1 call to _field_patterns_prepare_instance()
field_patterns_prepare in patterns_components/components/field.inc

File

patterns_components/components/field.inc, line 298
Patterns component for Fields.

Code

function _field_patterns_prepare_instance($action, $data) {
  $prefix = '';
  if ($action == PATTERNS_DELETE || $action == PATTERNS_MODIFY) {
    $prefix = 'field_';
  }
  $existing_f = '_add_existing_field';
  $new_f = '_add_new_field';
  $all_fields = field_info_fields();
  $existing = array_key_exists($prefix . $data['name'], $all_fields) ? $existing_f : $new_f;
  $data['fields'] = array();
  $fname = $prefix . $data['name'];

  // Setting default values which can be overriden later
  $default_type = 'text';

  //text_textfield';

  // $default_parent = '';
  $default_weight = 1;
  $default_widget_type = 'text_textfield';
  $default_cardinality = 1;
  $default_label = $fname;
  if ($action == PATTERNS_CREATE) {

    // NAME
    $data['fields'][$existing]['field_name'] = $fname;
    unset($data['name']);

    // LABEL
    if (isset($data['label'])) {
      $data['fields'][$existing]['label'] = $data['label'];
      unset($data['label']);
    }
    else {
      $data['fields'][$existing]['label'] = $default_label;
    }

    // WEIGHT
    if (isset($data['weight'])) {
      $data['fields'][$existing]['weight'] = $data['weight'];
      unset($data['weight']);
    }
    else {
      $data['fields'][$existing]['weight'] = $default_weight;
    }

    // PARENT(unused parameter?)

    /*
         if (isset($data['parent'])) {
         $data['fields'][$EXISTING]['parent'] = $data['parent'];
         unset($data['parent']);
         }
         else {
         $data['fields'][$EXISTING]['parent'] = $default_parent;
        }*/

    // TYPE
    if (isset($data['type'])) {
      $data['fields'][$existing]['type'] = $data['type'];
      unset($data['type']);
    }
    else {
      $data['fields'][$existing]['type'] = $default_type;
    }

    // WIDGET
    // @TODO: check for incompatible widget_type and field_type
    // @TODO: add aliases for long names
    // Term Reference:
    // taxonomy_autocomplete
    // options_buttons
    if (isset($data['widget'])) {
      switch ($data['widget']) {
        case 'autocomplete':
          $w = 'taxonomy_autocomplete';
          break;
        case 'buttons':
          $w = 'options_buttons';
          break;
        default:
          $w = $data['widget'];
      }
      $data['fields'][$existing]['widget_type'] = $w;
      unset($data['widget']);
    }
    else {
      $data['fields'][$existing]['widget_type'] = $default_widget_type;
    }
  }
  elseif ($action == PATTERNS_MODIFY) {

    // INSTANCE
    if (isset($data['label'])) {
      $data['instance']['label'] = $data['label'];
    }

    // REQUIRED
    if (isset($data['required']) && $data['required']) {
      $data['instance']['required'] = 1;
      unset($data['required']);
    }

    // HELP DESCR
    if (isset($data['description'])) {
      $data['instance']['description'] = $data['description'];
      unset($data['description']);
    }

    // CARDINALITY
    if (isset($data['cardinality'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['field']['cardinality'] = $data['cardinality'];
      unset($data['cardinality']);
    }
    else {
      $data['field']['cardinality'] = $default_cardinality;
    }
    if (isset($data['text_processing'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['instance']['settings']['text_processing'] = $data['text_processing'];
      unset($data['text_processing']);
    }
    if (isset($data['max_length'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['field']['settings']['max_length'] = $data['max_length'];
      unset($data['max_length']);
    }
    if (isset($data['summary'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['instance']['settings']['display_summary'] = $data['summary'];
      unset($data['summary']);
    }
    if (isset($data['rows'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['instance']['widget']['settings']['rows'] = $data['rows'];
      $data['instance']['widget']['active'] = 1;
      unset($data['rows']);
    }
    if (isset($data['size'])) {

      //$c = array('und' => array(array('value' =>$data['cardinality'],'safe_value'=>$data['cardinality'])));
      $data['instance']['widget']['settings']['size'] = $data['size'];
      $data['instance']['widget']['active'] = 1;
      unset($data['size']);
    }

    //default_value
    if (isset($data['default_value'])) {
      $data[$fname]['und'][0]['value'] = $data['default_value'];
      unset($data['default_value']);
    }

    //other special field type settings form
    if (isset($data['instance_settings'])) {
      foreach ($data['instance_settings'] as $key => $setting) {
        if (is_array($setting)) {
          foreach ($setting as $key1 => $set) {
            $data['instance']['settings'][$key][$key1] = $set;
          }
        }
        else {
          $data['instance']['settings'][$key] = $setting;
        }
      }
      unset($data['instance_settings']);
    }
    if (isset($data['field_settings'])) {
      foreach ($data['field_settings'] as $key => $setting) {
        if (is_array($setting)) {
          foreach ($setting as $key1 => $set) {
            $data['field']['settings'][$key][$key1] = $set;
          }
        }
        else {
          $data['field']['settings'][$key] = $setting;
        }
      }
      unset($data['field_settings']);
    }
    if (isset($data['widget_settings'])) {
      foreach ($data['widget_settings'] as $key => $setting) {
        if (is_array($setting)) {
          foreach ($setting as $key1 => $set) {
            $data['instance']['widget']['settings'][$key][$key1] = $set;
          }
        }
        else {
          $data['instance']['widget']['settings'][$key] = $setting;
        }
      }
      $data['instance']['widget']['active'] = 1;
      unset($data['widget_settings']);
    }
  }
  return $data;
}