You are here

function flexiform_manage_form_entities_form in Flexiform 7

flexiform_manage_form_entities_form

This form configures the entities involved in a flexiform.

1 string reference to 'flexiform_manage_form_entities_form'
FlexiformUIController::hook_menu in ./flexiform.admin.inc
Overrides hook_menu() defaults.

File

./flexiform.admin.inc, line 1265
Model type editing UI.

Code

function flexiform_manage_form_entities_form($form, &$form_state, $flexiform) {

  // Get the list of entities
  $entities = $flexiform->entities;
  $form += array(
    '#flexiform' => $flexiform,
    '#entities' => array_keys($entities),
  );
  $form_state += array(
    '#flexiform' => $flexiform,
    '#entities' => $entities,
  );
  $table = array(
    '#type' => 'flexiform_entity_table',
    '#tree' => TRUE,
    '#header' => array(
      t('Label'),
      t('Weight'),
      t('Namespace'),
      t('Entity Type'),
      t('Bundle'),
      t('Getter'),
      array(
        'data' => t('Operations'),
        'colspan' => 2,
      ),
    ),
    '#parent_options' => array(),
    '#regions' => array(
      'main' => array(
        'message' => t('No entities are present'),
      ),
      'add_new' => array(
        'title' => ' ',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'field-ui-overview',
      ),
      'id' => 'field-overview',
    ),
  );
  $getters = flexiform_entity_getter_info();
  foreach ($entities as $entity_namespace => $settings) {
    $entity_path = 'admin/structure/flexiforms/manage/' . $flexiform->form . '/form-entities/' . $entity_namespace;
    $table[$entity_namespace] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
          'tabledrag-leaf',
        ),
      ),
      '#row_type' => 'entity',
      '#region_callback' => 'flexiform_entity_form_row_region',
      'label' => array(
        '#markup' => check_plain($settings['label']),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#title' => t('Weight for @title', array(
          '@title' => $settings['label'],
        )),
        '#title_display' => 'invisible',
        '#default_value' => $settings['weight'],
        '#size' => 3,
        '#attributes' => array(
          'class' => array(
            'field-weight',
          ),
        ),
      ),
      'namespace' => array(
        '#markup' => $entity_namespace,
      ),
      'entity_type' => array(
        '#markup' => $settings['entity_type'],
      ),
      'bundle' => array(
        '#markup' => $settings['bundle'],
      ),
      'getter' => array(
        '#markup' => $settings['getter'],
      ),
      'configure' => array(
        '#type' => 'link',
        '#title' => t('configure'),
        '#href' => $entity_path,
        '#options' => array(
          'attributes' => array(
            'title' => t('Configure entity getter settings'),
          ),
        ),
      ),
      'remove' => array(
        '#type' => 'link',
        '#title' => t('remove'),
        '#href' => $entity_path . '/remove',
        '#options' => array(
          'attributes' => array(
            'title' => t('Remove this entity.'),
          ),
        ),
      ),
    );
  }

  // Additional row: add new field.
  $max_weight = flexiform_entity_max_weight($flexiform);

  // Entity type options
  $entity_type_options = array();
  foreach (entity_get_info() as $entity_type => $info) {
    $entity_type_options[$entity_type] = $info['label'];
  }

  // Get entity type if its been set
  if (isset($form_state['values']['entities']['_add_entity']['entity_type'])) {
    $entity_type = $form_state['values']['entities']['_add_entity']['entity_type'];
  }
  else {
    $entity_type = NULL;
  }

  // Get bundle options
  $bundle_options = array();
  if (!empty($entity_type)) {
    $entity_info = entity_get_info($entity_type);
    foreach ($entity_info['bundles'] as $bundle => $info) {
      $bundle_options[$bundle] = $info['label'];
    }
  }

  // Get getter options
  $getter_options = array();
  if (!empty($entity_type)) {
    foreach (flexiform_entity_type_get_getters($entity_type) as $getter => $info) {
      if (!empty($info['params'])) {

        // Put the available entities into
        $entities_by_type = array();
        foreach ($flexiform->entities as $entity_namespace => $settings) {
          $entities_by_type[$settings['entity_type']][] = $entity_namespace;
        }
        $combinations = array();
        flexiform_get_getter_parameter_combinations($info['params'], $entities_by_type, $combinations);
        foreach ($combinations as $combination) {
          $key_bits = array(
            $getter,
          );
          $label_bits = array();
          $label = t('@label from ', array(
            '@label' => $info['label'],
          ));
          foreach ($combination as $param => $namespace) {
            $key_bits[] = $param . '=' . $namespace;
            $label_bits[] = t('!label (@param)', array(
              '!label' => $flexiform->entities[$namespace]['label'],
              '@param' => $param,
            ));
          }
          $key = implode(':', $key_bits);
          $label .= implode(', ', $label_bits);
          $getter_options[$key] = $label;
        }
      }
      else {
        $getter_options[$getter] = $info['label'];
      }
    }
  }
  if ($entity_type_options) {
    $name = '_add_entity';
    $table[$name] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
          'tabledrag-leaf',
          'add-new',
        ),
      ),
      '#row_type' => 'add_entity',
      '#region_callback' => 'flexiform_entity_form_row_region',
      'label' => array(
        '#type' => 'textfield',
        '#title' => 'Label',
        '#title_display' => 'invisible',
        '#size' => 15,
        '#description' => t('Label'),
        '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add entity') . '</div>',
        '#suffix' => '</div>',
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $max_weight + 1,
        '#size' => 3,
        '#title_display' => 'invisible',
        '#title' => t('Weight for new entity'),
        '#attributes' => array(
          'class' => array(
            'field-weight',
          ),
        ),
        '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
      ),
      'namespace' => array(
        '#type' => 'textfield',
        '#title' => 'Namespace',
        '#title_display' => 'invisible',
        '#size' => 15,
        '#description' => t('namespace'),
      ),
      'entity_type' => array(
        '#type' => 'select',
        '#title' => t('Enitity Type'),
        '#title_display' => 'invisible',
        '#options' => $entity_type_options,
        '#empty_option' => t('- Select an Entity Type -'),
        '#description' => t('Type of entity'),
        '#attributes' => array(
          'class' => array(
            'bundle-select',
          ),
        ),
        '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        '#ajax' => array(
          'event' => 'change',
          'method' => 'replace',
          'wrapper' => 'flexiform-bundle-selector',
          'callback' => 'flexiform_manage_form_entities_bundles',
        ),
      ),
      'bundle' => array(
        '#type' => 'select',
        '#title' => t('Bundle'),
        '#title_display' => 'invisible',
        '#options' => $bundle_options,
        '#empty_option' => t('- Select a Bundle -'),
        '#description' => t('Bundle'),
        '#attributes' => array(
          'class' => array(
            'bundle-select',
          ),
        ),
        '#prefix' => '<div id="flexiform-bundle-selector"><div class="add-new-placeholder">&nbsp;</div>',
        '#suffix' => '</div>',
        '#ajax' => array(
          'event' => 'change',
          'method' => 'replace',
          'wrapper' => 'flexiform-getter-selector',
          'callback' => 'flexiform_manage_form_entities_getters',
        ),
      ),
      'getter' => array(
        '#type' => 'select',
        '#title' => t('Getter'),
        '#title_display' => 'invisible',
        '#options' => $getter_options,
        '#empty_option' => t('- Select a Getter -'),
        '#description' => t('Pick a getter'),
        '#attributes' => array(
          'class' => array(
            'getter-select',
          ),
        ),
        '#prefix' => '<div id="flexiform-getter-selector"><div class="add-new-placeholder">&nbsp;</div>',
        '#suffix' => '</div>',
      ),
    );
  }
  $form['entities'] = $table;
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['#attached']['css'][] = drupal_get_path('module', 'flexiform') . '/flexiform_ui.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'flexiform') . '/flexiform_ui.js';

  // Add tabledrag behavior.
  $form['#attached']['drupal_add_tabledrag'][] = array(
    'field-overview',
    'order',
    'sibling',
    'field-weight',
  );
  $form['#attached']['drupal_add_tabledrag'][] = array(
    'field-overview',
    'match',
    'parent',
    'field-parent',
    'field-parent',
    'field-name',
  );
  return $form;
}