You are here

function eck__manage_extra_field_form in Entity Construction Kit (ECK) 7.3

Form for managing the extra field settings for a given entity property.

Parameters

array $form: The entity property extra field management form. For configuration of extra field settings like its label.

array $form_state: The current state of the form.

string $entity_type_name: The machine name of the entity type.

string $bundle_type: The machine name of the bundle type.

string $property_name: The machine name of the entity property being included as an extra field.

Return value

array An array comprising the form for managing the settings for the extra field.

1 string reference to 'eck__manage_extra_field_form'
eck__entity__menu in ./eck.entity.inc
Entity related menu items.

File

./eck.bundle.inc, line 509

Code

function eck__manage_extra_field_form($form, &$form_state, $entity_type_name, $bundle_type, $property_name) {
  $entity_type = entity_type_load($entity_type_name);
  $bundle = bundle_load($entity_type_name, $bundle_type);
  drupal_set_title($bundle->config['extra_fields'][$property_name]['form']['label']);
  if (empty($bundle->config['extra_fields'][$property_name]['form'])) {
    $form_state['redirect'] = 'admin/structure/entity-type/' . $entity_type_name . '/' . $bundle->name . '/fields';
    return array();
  }
  $bundle_property_config = $bundle->config['extra_fields'][$property_name]['form'];
  $widget_type = eck_property_info_widget_types($bundle_property_config['widget']['type']);
  $form = array(
    '#entity_type' => $entity_type,
    '#bundle' => $bundle,
    '#property_name' => $property_name,
  );

  // Build the configurable property extra field form values.
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#default_value' => !empty($bundle_property_config['label']) ? $bundle_property_config['label'] : $property_name,
    '#required' => TRUE,
    '#weight' => -20,
  );
  $form['required'] = array(
    '#type' => 'checkbox',
    '#title' => t('Required field'),
    '#default_value' => !empty($bundle_property_config['required']),
    '#weight' => -10,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Help text'),
    '#default_value' => !empty($bundle_property_config['description']) ? $bundle_property_config['description'] : '',
    '#rows' => 5,
    '#description' => t('Instructions to present to the user below this property field on the editing form.<br />Allowed HTML tags: @tags', array(
      '@tags' => _field_filter_xss_display_allowed_tags(),
    )),
    '#weight' => -5,
  );

  // 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']);
  }

  // Add additional widget settings from the property's widget type module.
  $additions = module_invoke($widget_type['module'], 'eck_property_widget_settings_form', $entity_type, $bundle, $property_name, $bundle_property_config);
  if (is_array($additions)) {
    $form['widget'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#title' => t('%type widget settings', array(
        '%type' => $widget_type['label'],
      )),
    );
    $form['widget']['settings'] = $additions;
  }

  // Add handling for default value if not provided by any other module.
  if (empty($bundle_property_config['default_value_function'])) {
    $function = $widget_type['module'] . '_eck_property_widget_form';
    if (function_exists($function)) {
      $form['default_value_widget'] = array(
        '#type' => 'fieldset',
        '#title' => t('Default value'),
        '#collapsible' => FALSE,
        '#tree' => TRUE,
        '#description' => t('The default value for this property field, used when creating new content.'),
        // Stick to an empty 'parents' on this form in order not to breaks
        // widgets that do not use field_widget_[field|instance]() and still
        // access $form_state['field'] directly.
        '#parents' => array(),
      );
      $element = array(
        '#entity' => NULL,
        '#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']),
      );

      // Populate widgets with default values when creating a new entity.
      if ($element = $function($form, $form_state, $property_name, $bundle_property_config, LANGUAGE_NONE, $bundle_property_config['default_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' => $bundle_property_config['default_value'],
        );
        drupal_alter(array(
          'eck_property_widget_form',
          'eck_property_widget_' . $bundle_property_config['widget']['type'] . '_form',
        ), $element, $form_state, $context);
      }
      $form['default_value_widget'][$property_name] = $element;
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Continue'),
  );
  return $form;
}