You are here

public function CivicrmInlineEntityFormController::entityForm in CiviCRM Entity 7.2

Overrides EntityInlineEntityFormController::entityForm().

Overrides EntityInlineEntityFormController::entityForm

File

modules/civicrm_entity_inline/includes/civicrm.civicrm_entity_inline.inc, line 7

Class

CivicrmInlineEntityFormController

Code

public function entityForm($entity_form, &$form_state) {
  $entity_type = $entity_form['#entity_type'];
  $entity = $entity_form['#entity'];
  $entity->type = $entity_type;
  $op = $entity_form['#op'];
  field_attach_form($entity_type, $entity, $entity_form, $form_state);
  if ($op == 'edit') {
    if (is_object($entity)) {
      $entity->original = clone $entity;
    }
  }

  //get entity controller build content
  if ($op == 'edit') {
    $wrapper = entity_metadata_wrapper($entity_type, $entity);

    //$wrapper = entity_metadata_wrapper($entity_type, $entity_id);
  }
  else {
    $wrapper = entity_metadata_wrapper($entity_type);
  }
  foreach ($wrapper as $name => $child) {
    $info = $child
      ->info();
    if (strpos($info['name'], 'field_') === 0) {
      continue;
    }
    else {
      if (isset($info['widget']['widget'])) {
        $entity_form[$name] = array(
          '#type' => $info['widget']['widget'],
          '#title' => $info['label'],
          '#description' => !empty($info['description']) ? $info['description'] : '',
        );

        // set api required fields to be required on form
        if (!empty($info['required'])) {
          $entity_form[$name]['#required'] = TRUE;
        }

        // FK Reference field handling
        if ($entity_form[$name]['#type'] == 'civi_fk_reference') {
          switch ($info['widget']['subtype']) {
            case 'autocomplete':
              $entity_form[$name]['#type'] = 'textfield';
              $entity_form[$name]['#autocomplete_path'] = 'civicrm-entity/autocomplete/' . $info['widget']['entity'];
              break;
            case 'select':
              $form[$name]['#type'] = 'select';
              $form[$name]['#options'] = !empty($entity_form[$name]['#required']) ? _civicrm_entity_get_entities_list($info['widget']['entity']) : array(
                NULL => 'None',
              ) + _civicrm_entity_get_entities_list($info['widget']['entity']);
              break;
            default:
              $entity_form[$name]['#type'] = 'textfield';
              break;
          }
        }
        if ($op == 'edit') {
          $entity_form[$name]['#default_value'] = $child
            ->value();
          if ($entity_type == 'civicrm_participant') {
            if ($name == 'status_id' && !empty($entity->participant_status_id)) {
              $entity_form[$name]['#default_value'] = $entity->participant_status_id;
            }
            if ($name == 'role_id' && !empty($entity->participant_role_id)) {
              $entity_form[$name]['#default_value'] = $entity->participant_role_id;
            }
            if ($name == 'source' && !empty($entity->participant_source)) {
              $entity_form[$name]['#default_value'] = $entity->participant_source;
            }
            if ($name == 'register_date' && !empty($entity->participant_register_date)) {
              $entity_form[$name]['#default_value'] = $entity->participant_register_date;
            }
            if ($name == 'registered_by_id' && !empty($entity->participant_registered_by_id)) {
              $entity_form[$name]['#default_value'] = $entity->participant_registered_by_id;
            }
            if ($name == 'fee_amount' && !empty($entity->participant_fee_amount)) {
              $entity_form[$name]['#default_value'] = $entity->participant_fee_amount;
            }
          }
        }
        elseif ($op == 'create' || $op == 'add') {
          if (isset($info['default_value'])) {
            $entity_form[$name]['#default_value'] = $info['default_value'];
          }
          else {
            $entity_form[$name]['#default_value'] = NULL;
          }
        }

        // set the #size and #maxlength FAPI properties if the #type is a textfield
        if ($entity_form[$name]['#type'] == 'textfield') {
          if (!empty($info['size'])) {
            $entity_form[$name]['#size'] = $info['size'];
          }
          if (!empty($info['maxlength'])) {
            $entity_form[$name]['#maxlength'] = $info['maxlength'];
          }
        }

        //set rows and cols if available for textarea or text_format
        if ($entity_form[$name]['#type'] == 'textarea' || $entity_form[$name]['#type'] == 'text_format') {
          if ($entity_form[$name]['#type'] == 'text_format') {
            $entity_form[$name]['#format'] = 'full_html';
          }

          // set rows value if not empty
          if (!empty($info['rows'])) {
            $entity_form[$name]['#rows'] = $info['rows'];
          }
          if (!empty($info['cols'])) {
            $entity_form[$name]['#cols'] = $info['cols'];
          }
        }

        //set the options for select options, radios, and checkboxes
        if ($entity_form[$name]['#type'] == 'select') {
          if (!empty($info['widget']['options'])) {
            $entity_form[$name]['#options'] = $info['widget']['options'];
            if (!empty($info['required'])) {
              if (isset($entity_form[$name]['#options'][''])) {
                unset($entity_form[$name]['#options']['']);
              }
            }
          }
        }
        elseif ($entity_form[$name]['#type'] == 'radios') {
          $entity_form[$name]['#options'] = $info['widget']['options'];
          if (!empty($info['required'])) {
            if (isset($entity_form[$name]['#options'][''])) {
              unset($entity_form[$name]['#options']['']);
            }
          }
        }
        elseif ($entity_form[$name]['#type'] == 'checkboxes') {
          if ($op == 'edit') {
            $value = $child
              ->value();
            if (!empty($value)) {
              foreach ($info['widget']['options'] as $key => $label) {
                if (in_array($key, $value)) {
                  $default_values[] = $key;
                }
              }
              $entity_form[$name]['#default_value'] = $default_values;
            }
            else {
              $entity_form[$name]['#default_value'] = array(
                '',
              );
            }
          }
          elseif ($op == 'create' || $op == 'add') {
            if (isset($info['default_value'])) {
              $entity_form[$name]['#default_value'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $entity_form[$name]['#default_value']);
            }
          }
          $entity_form[$name]['#options'] = $info['widget']['options'];
          if (!empty($info['required'])) {
            if (isset($entity_form[$name]['#options'][''])) {
              unset($entity_form[$name]['#options']['']);
            }
          }
        }
        elseif ($entity_form[$name]['#type'] == 'date_select') {
          if (module_exists('date_api')) {
            if (module_exists('date_popup')) {
              $entity_form[$name]['#type'] = 'date_popup';
            }
            $entity_form[$name]['#format'] = $info['widget']['format'];
            $entity_form[$name]['#date_year_range'] = '-115:+10';
          }
          else {
            $entity_form[$name]['#type'] = 'textfield';
          }
        }
        if ($entity_type == 'civicrm_contact') {

          //for some reason the is_deleted column of the contact record is coming to the entity

          // as contact_is_deleted ...special handling to have the form value set properly
          if ($name == 'is_deleted') {
            $entity_form[$name]['#default_value'] = isset($entity->contact_is_deleted) ? $entity->contact_is_deleted : 0;
          }
          if ($name == 'contact_type') {
            $entity_form[$name]['#ajax'] = array(
              'event' => 'change',
              'wrapper' => 'contact-subtype-wrapper',
              'callback' => '_civicrm_entity_form_contact_subtype_ajax_callback',
              'method' => 'replace',
            );
          }
          if ($name == 'contact_sub_type') {
            $contact_type = !empty($entity_form['contact_type']['#default_value']) ? $entity_form['contact_type']['#default_value'] : 'Individual';
            $entity_form[$name]['#type'] = 'select';
            $entity_form[$name]['#options'] = _civicrm_entity_form_contact_get_subtypes($contact_type);
            $entity_form[$name]['#prefix'] = '<div id="contact-subtype-wrapper">';
            $entity_form[$name]['#suffix'] = '</div>';
            $entity_form[$name]['#validated'] = TRUE;
            unset($entity_form[$name]['#size']);
          }
        }

        // event special handling
        if ($entity_type == 'civicrm_event') {

          //if($name == 'thankyou_text' || $name == 'thankyou_footer_text') {

          //  $entity_form[$name]['#type'] = 'text_format';
          // }
          if ($name == 'event_type_id') {
            $entity_form[$name]['#required'] = TRUE;
          }
          if ($name == 'default_role_id') {
            $entity_form[$name]['#required'] = TRUE;
          }
        }

        // make sure to disable id field, and any custom field with is_view = 1
        if ($name == 'id' || !empty($info['is_view'])) {
          $entity_form[$name]['#disabled'] = TRUE;
        }
      }

      // end if isset widget
    }

    // end else not a drupal field
  }

  // end foreach
  return $entity_form;
}