You are here

function eck_form_field_ui_field_overview_form_alter in Entity Construction Kit (ECK) 7.3

Adds a manage properties section.

To the field overview 'Manage fields' form that allows for the inclusion of properties as "extra fields" on entity forms.

File

./eck.module, line 763

Code

function eck_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
  if (array_key_exists($form['#entity_type'], eck_entity_info())) {
    $entity_type = entity_type_load($form['#entity_type']);
    $bundle = bundle_load($form['#entity_type'], $form['#bundle']);
    $admin_path = _field_ui_bundle_admin_path($bundle->entity_type, $bundle->name);
    $max_weight = field_info_max_weight($form['#entity_type'], $form['#bundle'], 'form');
    $widget_types = eck_property_info_widget_types();
    $widget_type_options = eck_property_widget_type_options(NULL, TRUE);
    $properties = array();
    $extra_fields = !empty($bundle->config['extra_fields']) && is_array($bundle->config['extra_fields']) ? $bundle->config['extra_fields'] : array();
    foreach ($entity_type->properties as $property => $info) {
      if (empty($extra_fields[$property]['form'])) {
        $properties[$property] = $info;
      }
      else {
        $admin_property_path = $admin_path . '/properties/' . $property;
        unset($form['fields'][$property]['type']['#cell_attributes']);
        unset($form['fields'][$property]['edit']);
        unset($form['fields'][$property]['delete']);
        $key = $extra_fields[$property]['form']['widget']['type'];
        $table_elements = array(
          'widget_type' => array(
            '#type' => 'link',
            '#title' => t("@widget_label", array(
              "@widget_label" => $widget_types[$key]['label'],
            )),
            '#href' => $admin_property_path . '/widget-type',
            '#options' => array(
              'attributes' => array(
                'title' => t('Change widget type.'),
              ),
            ),
          ),
          'edit' => array(
            '#type' => 'link',
            '#title' => t('edit'),
            '#href' => $admin_property_path,
            '#options' => array(
              'attributes' => array(
                'title' => t('Edit property management settings.'),
              ),
            ),
          ),
          'delete' => array(
            '#type' => 'link',
            '#title' => t('remove'),
            '#href' => $admin_property_path . '/remove',
            '#options' => array(
              'attributes' => array(
                'title' => t('Remove management of this property.'),
              ),
            ),
          ),
        );
        $form['fields'][$property] += $table_elements;
      }
    }

    // Additional row: add properties as extra fields.
    if ($properties && $widget_type_options) {

      // Build list of options.
      $properties_options = array();
      foreach ($properties as $property => $info) {
        $text = t('@type: @label (@property)', array(
          '@type' => $info['type'],
          '@label' => $info['label'],
          '@property' => $property,
        ));
        $properties_options[$property] = truncate_utf8($text, 80, FALSE, TRUE);
      }
      asort($properties_options);
      $name = '_eck_add_extra_field';
      $form['fields'][$name] = array(
        '#attributes' => array(
          'class' => array(
            'draggable',
            'tabledrag-leaf',
            'add-new',
          ),
        ),
        '#row_type' => 'add_new_field',
        '#region_callback' => 'field_ui_field_overview_row_region',
        'label' => array(
          '#type' => 'textfield',
          '#title' => t('Extra field label'),
          '#title_display' => 'invisible',
          '#size' => 15,
          '#description' => t('Label'),
          '#attributes' => array(
            'class' => array(
              'label-textfield',
            ),
          ),
          '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Manage a property') . '</div>',
          '#suffix' => '</div>',
        ),
        'weight' => array(
          '#type' => 'textfield',
          '#default_value' => $max_weight + 3,
          '#size' => 3,
          '#title_display' => 'invisible',
          '#title' => t('Weight for added field'),
          '#attributes' => array(
            'class' => array(
              'field-weight',
            ),
          ),
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        ),
        'parent_wrapper' => array(
          'parent' => array(
            '#type' => 'select',
            '#title' => t('Parent for extra field'),
            '#title_display' => 'invisible',
            '#options' => $form['fields']['#parent_options'],
            '#empty_value' => '',
            '#attributes' => array(
              'class' => array(
                'field-parent',
              ),
            ),
            '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
            '#parents' => array(
              'fields',
              $name,
              'parent',
            ),
          ),
          'hidden_name' => array(
            '#type' => 'hidden',
            '#default_value' => $name,
            '#attributes' => array(
              'class' => array(
                'field-name',
              ),
            ),
          ),
        ),
        'field_name' => array(
          '#type' => 'select',
          '#title' => t('Extra field'),
          '#title_display' => 'invisible',
          '#options' => $properties_options,
          '#empty_option' => t('- Select a property -'),
          '#description' => t('Existing entity property to manage.'),
          '#attributes' => array(
            'class' => array(
              'eck-property-type-select',
            ),
          ),
          '#cell_attributes' => array(
            'colspan' => 2,
          ),
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        ),
        'widget_type' => array(
          '#type' => 'select',
          '#title' => t('Widget for managed property'),
          '#title_display' => 'invisible',
          '#options' => $widget_type_options,
          '#empty_option' => t('- Select a widget -'),
          '#description' => t('Form element to edit the property data.'),
          '#attributes' => array(
            'class' => array(
              'eck-widget-type-select',
            ),
          ),
          '#cell_attributes' => array(
            'colspan' => 3,
          ),
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        ),
      );

      // Include ECK specific validation and submit handling for this form.
      $form['#validate'][] = 'eck_form_field_ui_field_overview_form_validate';
      $form['#submit'][] = 'eck_form_field_ui_field_overview_form_submit';

      // Attach handling for property selection.
      $form['#attached']['js'][] = drupal_get_path('module', 'eck') . '/eck.field_ui.js';

      // Add settings for the update selects behavior.
      $js_properties = array();
      foreach ($properties as $property => $info) {
        $js_properties[$property] = array(
          'label' => $info['label'],
          'type' => $info['type'],
        );
      }
      $form['#attached']['js'][] = array(
        'type' => 'setting',
        'data' => array(
          'eckProperties' => $js_properties,
          'eckPropertyWidgetTypes' => eck_property_widget_type_options(),
        ),
      );
    }
  }
}