You are here

function relation_ui_type_form in Relation 7

Relation relation type bundle settings form.

Parameters

$relation_type: Relation type machine name. If this is not provided, assume that we're creating a new relation type.

1 string reference to 'relation_ui_type_form'
relation_ui_menu in ./relation_ui.module
Implements hook_menu().

File

./relation_ui.module, line 186
Provide administration interface for relation type bundles.

Code

function relation_ui_type_form($form, &$form_state, $relation_type = array(), $op = 'edit') {
  $form['#write_record_keys'] = array();
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'relation') . '/relation_ui.css',
  );
  if ($relation_type) {
    $relation_type = (object) $relation_type;
    if (empty($relation_type->in_code_only)) {
      $form['#write_record_keys'][] = 'relation_type';
    }
  }
  else {
    $relation_type = (object) array(
      'relation_type' => '',
      'label' => '',
      'reverse_label' => '',
      'bundles' => array(),
      'directional' => FALSE,
      'transitive' => FALSE,
      'r_unique' => FALSE,
      'min_arity' => 2,
      'max_arity' => 2,
      'source_bundles' => array(),
      'target_bundles' => array(),
    );
  }
  $form['labels'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'relation-type-form-table',
      ),
    ),
    '#suffix' => '<div class="clearfix"></div>',
  );
  $form['labels']['name'] = array(
    // use 'name' for /misc/machine-name.js
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#description' => t('Display name of the relation type. This is also used as the predicate in natural language formatters (ie. if A is related to B, you get "A [label] B")'),
    '#default_value' => $relation_type->label,
    '#size' => 40,
    '#required' => TRUE,
  );
  $form['labels']['relation_type'] = array(
    '#type' => 'machine_name',
    '#default_value' => $relation_type->relation_type,
    '#maxlength' => 32,
    '#disabled' => $relation_type->relation_type,
    '#machine_name' => array(
      'source' => array(
        'labels',
        'name',
      ),
      'exists' => 'relation_type_load',
    ),
  );
  $form['labels']['reverse_label'] = array(
    '#type' => 'textfield',
    '#size' => 40,
    '#title' => t('Reverse label'),
    '#description' => t('Reverse label of the relation type. This is used as the predicate by formatters of directional relations, when you need to display the reverse direction (ie. from the target entity to the source entity). If this is not supplied, the forward label is used.'),
    '#default_value' => $relation_type->reverse_label,
    '#states' => array(
      'visible' => array(
        ':input[name="directional"]' => array(
          'checked' => TRUE,
        ),
        ':input[name="advanced[max_arity]"]' => array(
          '!value' => '1',
        ),
      ),
      'required' => array(
        ':input[name="directional"]' => array(
          'checked' => TRUE,
        ),
        ':input[name="advanced[max_arity]"]' => array(
          '!value' => '1',
        ),
      ),
    ),
  );
  $form['directional'] = array(
    '#type' => 'checkbox',
    '#title' => 'Directional',
    '#description' => t('A directional relation is one that does not imply the same relation in the reverse direction. For example, a "likes" relation is directional (A likes B does not neccesarily mean B likes A), whereas a "similar to" relation is non-directional (A similar to B implies B similar to A. Non-directional relations are also known as symmetric relations.'),
    '#default_value' => $relation_type->directional,
    '#states' => array(
      'invisible' => array(
        ':input[name="advanced[max_arity]"]' => array(
          'value' => '1',
        ),
      ),
    ),
  );

  // More advanced options, hide by default.
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 50,
    '#tree' => TRUE,
  );
  $form['advanced']['transitive'] = array(
    '#type' => 'checkbox',
    '#title' => t('Transitive'),
    '#description' => t('A transitive relation implies that the relation passes through intermediate entities (ie. A=>B and B=>C implies that A=>C). For example "Ancestor" is transitive: your ancestor\'s ancestor is also your ancestor. But a "Parent" relation is non-transitive: your parent\'s parent is not your parent, but your grand-parent.'),
    '#default_value' => $relation_type->transitive,
    '#states' => array(
      'invisible' => array(
        ':input[name="advanced[max_arity]"]' => array(
          'value' => '1',
        ),
      ),
    ),
  );
  $form['advanced']['r_unique'] = array(
    '#type' => 'checkbox',
    '#title' => t('Unique'),
    '#description' => t('Whether relations of this type are unique (ie. they can not contain exactly the same end points as other relations of this type).'),
    '#default_value' => $relation_type->r_unique,
  );

  // these should probably be changed to numerical (validated) textfields.
  $options = array(
    '1' => '1',
    '2' => '2',
    '3' => '3',
    '4' => '4',
    '5' => '5',
    '6' => '6',
    '7' => '7',
    '8' => '8',
  );
  $form['advanced']['min_arity'] = array(
    '#type' => 'select',
    '#title' => t('Minimum Arity'),
    '#options' => $options,
    '#description' => t('Minimum number of entities joined by relations of this type (e.g. three siblings in one relation). <em>In nearly all cases you will want to leave this set to 2</em>.'),
    '#default_value' => $relation_type->min_arity ? $relation_type->min_arity : 2,
  );
  $options = array(
    '1' => '1',
    '2' => '2',
    '3' => '3',
    '4' => '4',
    '5' => '5',
    '6' => '6',
    '7' => '7',
    '8' => '8',
    '0' => t('Infinite'),
  );
  $form['advanced']['max_arity'] = array(
    '#type' => 'select',
    '#title' => t('Maximum Arity'),
    '#options' => $options,
    '#description' => t('Maximum number of entities joined by relations of this type. <em>In nearly all cases you will want to leave this set to 2</em>.'),
    '#default_value' => isset($relation_type->max_arity) ? $relation_type->max_arity : 2,
  );
  $counter = 0;
  foreach (entity_get_info() as $entity_type => $entity) {
    $bundles[$entity['label']]["{$entity_type}:*"] = 'all ' . $entity['label'] . ' bundles';
    $counter += 2;
    if (isset($entity['bundles'])) {
      foreach ($entity['bundles'] as $bundle_id => $bundle) {
        $bundles[$entity['label']]["{$entity_type}:{$bundle_id}"] = $bundle['label'];
        $counter++;
      }
    }
  }
  $form['endpoints'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'relation-type-form-table',
      ),
    ),
    '#suffix' => '<div class="clearfix"></div>',
  );
  $form['endpoints']['source_bundles'] = array(
    '#type' => 'select',
    '#title' => t('Available source bundles'),
    '#options' => $bundles,
    '#size' => max(12, $counter),
    '#default_value' => $relation_type->source_bundles,
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#description' => 'Bundles that are not selected will not be available as sources for directional, or end points of non-directional relations relations. Ctrl+click to select multiple. Note that selecting all bundles also include bundles not yet created for that entity type.',
  );
  $form['endpoints']['target_bundles'] = array(
    '#type' => 'select',
    '#title' => t('Available target bundles'),
    '#options' => $bundles,
    '#size' => max(12, $counter),
    '#default_value' => $relation_type->target_bundles,
    '#multiple' => TRUE,
    '#description' => 'Bundles that are not selected will not be available as targets for directional relations. Ctrl+click to select multiple.',
    '#states' => array(
      'visible' => array(
        ':input[name="directional"]' => array(
          'checked' => TRUE,
        ),
        ':input[name="advanced[max_arity]"]' => array(
          '!value' => '1',
        ),
      ),
      'required' => array(
        ':input[name="directional"]' => array(
          'checked' => TRUE,
        ),
        ':input[name="advanced[max_arity]"]' => array(
          '!value' => '1',
        ),
      ),
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#weight' => 100,
    '#value' => t('Save'),
  );
  return $form;
}