You are here

function eck__entity_type__form in Entity Construction Kit (ECK) 7

Same name and namespace in other branches
  1. 7.3 eck.entity_type.inc \eck__entity_type__form()
  2. 7.2 eck.entity_type.inc \eck__entity_type__form()

Callback for adding entity types functionality.

Parameters

$form: Form array provided by the Form API

$state: array provided by the Form API

$entity_type_name: (String) the name of an existing entity type

2 string references to 'eck__entity_type__form'
eck__bundle__menu in ./eck.bundle.inc
This function creates the menu items relevant to bundle administration
eck__entity_type__menu in ./eck.entity_type.inc
Passthrough from hook_menu().

File

./eck.entity_type.inc, line 124
ENTITY TYPE

Code

function eck__entity_type__form($form, &$state, $entity_type_name = NULL) {
  if (!isset($entity_type_name)) {
    $entity_type = (object) array(
      'is_new' => TRUE,
      'label' => '',
      'name' => '',
      'type_label' => '',
      'type' => '',
      'properties' => array(),
      'custom_properties' => array(),
    );
  }
  else {
    $entity_type = eck__entity_type__load($entity_type_name);
    $entity_type->is_new = FALSE;
  }
  $form['entity_type'] = array(
    '#type' => 'value',
    '#value' => $entity_type,
  );
  $form['entity_type_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Entity Type'),
    '#default_value' => $entity_type->label,
    '#description' => t('A human readable name for the entity type'),
    '#required' => TRUE,
    '#disabled' => empty($entity_type->is_new),
  );
  $form['entity_type_name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $entity_type->name,
    '#disabled' => empty($entity_type->is_new),
    '#machine_name' => array(
      'exists' => '_eck_fake_exists',
      'source' => array(
        'entity_type_label',
      ),
    ),
  );
  $form['#validate'][] = 'eck__entity_type__form_validate';

  // Only allow entering the desired bundle name when creating a new entity.
  if (!empty($entity_type->is_new)) {
    $form['bundle_label'] = array(
      '#type' => 'textfield',
      '#title' => 'Bundle (optional)',
      '#description' => 'A bundle with the same name as the entity type is created by default, this will override the default',
    );
    $form['bundle_name'] = array(
      '#type' => 'machine_name',
      '#required' => FALSE,
      '#machine_name' => array(
        'exists' => '_eck_fake_exists',
        'source' => array(
          'bundle_label',
        ),
      ),
    );
  }

  // Enable/disable properties.
  $form['properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('Properties'),
    '#description' => t('Note: unchecking will delete the property and all data stored within!'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  module_load_include('inc', 'eck', 'eck.properties');
  foreach (eck_default_properties() as $property => $label) {
    $form['properties'][$property] = array(
      '#type' => 'checkbox',
      '#title' => $label,
      '#default_value' => !empty($entity_type->properties[$property]) || !empty($entity_type->is_new) ? 1 : 0,
    );
  }

  // Enable/disable custom properties.
  $form['custom_properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('Custom properties'),
    '#description' => t('Note: unchecking will delete the property and all data stored within!'),
    '#access' => !empty($entity_type->custom_properties),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  foreach ($entity_type->custom_properties as $property => $info) {
    $form['custom_properties'][$property] = array(
      '#type' => 'checkbox',
      '#title' => eck__entity_type__custom_property_label($info),
      '#default_value' => 1,
    );
  }

  // Add custom property.
  $form['add'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add custom property'),
    '#access' => empty($entity_type->is_new),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $types[t('Generic')] = array(
    'text' => t('Text'),
    'decimal' => t('Decimal'),
    'integer' => t('Integer'),
    'date' => t('Timestamp'),
  );
  foreach (entity_get_info() as $name => $data) {
    $types[t('Entities')][$name] = $data['label'];
  }
  $form['add']['type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => array(
      '' => t('- Please choose -'),
    ) + $types,
  );
  $form['add']['label'] = array(
    '#type' => 'textfield',
    '#title' => t("Name"),
    '#description' => t("A human readable name for the property."),
  );
  $form['add']['name'] = array(
    '#type' => 'machine_name',
    '#required' => FALSE,
    '#machine_name' => array(
      'label' => t("Database column name"),
      'exists' => '_eck_fake_exists',
      'source' => array(
        'add',
        'label',
      ),
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#weight' => 10000,
    '#value' => t('Save'),
  );
  return $form;
}