You are here

function eck__entity__form in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7 eck.entity.inc \eck__entity__form()
  2. 7.2 eck.entity.inc \eck__entity__form()

Sets up an entities form.

1 call to eck__entity__form()
EckInlineEntityFormController::entityForm in includes/eck.inline_entity_form.inc
Overrides EntityInlineEntityFormController::entityForm().
2 string references to 'eck__entity__form'
eck_forms in ./eck.module
Implements hook_forms().
eck__entity_type__info in ./eck.entity_type.inc
Generate the entity info for a specific entity.

File

./eck.entity.inc, line 366
All the menus, pages, and functionality related to administering entities.

Code

function eck__entity__form($form, &$form_state, $entity) {
  $form['entity'] = array(
    '#type' => 'value',
    '#value' => $entity,
  );

  // Property Widget Handling.
  $entity_type = entity_type_load($entity
    ->entityType());
  $bundle = bundle_load($entity_type->name, $entity->type);
  $property_info = entity_get_property_info($entity
    ->entityType());
  $language = LANGUAGE_NONE;
  if (function_exists("entity_language")) {
    $language = entity_language($entity_type->name, $entity);
  }
  $properties = array();
  foreach ($entity_type->properties as $property_name => $info) {
    if (empty($bundle->config['extra_fields'][$property_name]['form'])) {
      continue;
    }
    $bundle_property_config = $bundle->config['extra_fields'][$property_name]['form'];
    $widget_type = eck_property_info_widget_types($bundle_property_config['widget']['type']);

    // Get the default value for this property.
    $value = NULL;
    if (isset($entity->{$property_name})) {
      $value = $entity->{$property_name};
    }
    elseif (isset($bundle_property_config['default_value_function'])) {
      $value = $bundle_property_config['default_value_function']($entity_type, $bundle, $entity);
    }
    elseif ($bundle_property_config['default_value']) {
      $value = $bundle_property_config['default_value'];
    }

    // Include external module file dependency if one is required.
    if (function_exists('drupal_get_path') && $widget_type['file']) {
      form_load_include($form_state, $widget_type['file type'], $widget_type['module'], $widget_type['file']);
    }
    $function = $widget_type['module'] . '_eck_property_widget_form';
    if (function_exists($function)) {
      $element = array(
        '#entity' => $entity,
        '#entity_type' => $entity_type,
        '#bundle' => $bundle,
        '#property_name' => $property_name,
        '#widget_type' => $widget_type,
        '#language' => LANGUAGE_NONE,
        '#title' => check_plain($bundle_property_config['label']),
        '#description' => field_filter_xss($bundle_property_config['description']),
        '#required' => !empty($bundle_property_config['required']),
      );

      // Call the widget's form hook and load the widget form element.
      if ($element = $function($form, $form_state, $property_name, $bundle_property_config, $language, $value, $element)) {

        // Allow modules to alter the property widget form element.
        $context = array(
          'form' => $form,
          'property_name' => $property_name,
          'bundle_property_config' => $bundle->config['extra_fields'][$property_name],
          'langcode' => LANGUAGE_NONE,
          'value' => $value,
        );
        drupal_alter(array(
          'eck_property_widget_form',
          'eck_property_widget_' . $bundle_property_config['widget']['type'] . '_form',
        ), $element, $form_state, $context);
      }
      $properties[$property_name] = $element;
    }
  }

  // Check the property behaviors for widgets too.
  // @todo Can probably integrate this with the widget hooks and do away
  // with this.
  $vars = array(
    'entity' => $entity,
  );
  $vars += $property_info;

  // Add the property forms to the entity form.
  if (!empty($properties)) {
    $form += $properties;
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#weight' => 10000,
    '#value' => t('Save'),
  );
  field_attach_form($entity
    ->entityType(), $entity, $form, $form_state);
  return $form;
}