function og_vocab_form_taxonomy_form_vocabulary_alter in OG Vocabulary 7
Same name and namespace in other branches
- 6 og_vocab.module \og_vocab_form_taxonomy_form_vocabulary_alter()
Implements hook_form_FORM-ID_alter().
1) Add group id to the vocabulary if it's a group context. 2) Add own submit handler.
See also
og_vocab_form_taxonomy_form_vocabulary_submit().
File
- ./
og_vocab.module, line 1051 - Give each group its own system controlled vocabularies.
Code
function og_vocab_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
if (!empty($form_state['triggering_element']['#parents'][0]) && $form_state['triggering_element']['#parents'][0] == 'delete') {
// This is delete confirmation page.
return;
}
if (!empty($form['#vocabulary']->vid)) {
if (!($relation = og_vocab_relation_get($form['#vocabulary']->vid))) {
// This is an existing vocabulary, not associated with a group.
return;
}
$group_type = $relation->group_type;
$gid = $relation->gid;
}
elseif ($context = og_vocab_is_group_admin_context()) {
// We are inside a group context.
$group_type = $context['group_type'];
$gid = $context['gid'];
$form['#validate'][] = 'og_vocab_form_taxonomy_form_vocabulary_validate';
}
if (empty($group_type)) {
// We don't have enough context, about which group this vocabulary
// should belong to.
return;
}
$group = entity_load_single($group_type, $gid);
list(, , $group_bundle) = entity_extract_ids($group_type, $group);
// Validate the machine name when creating a new vocabulary.
if (empty($form_state['vocabulary']->vid)) {
$form['machine_name']['#element_validate'][] = 'og_vocab_machine_name_validate';
}
$form['og_vocab_relation'] = array(
'#type' => 'value',
'#value' => array(
'group_type' => $group_type,
'gid' => $gid,
),
);
// Add widget settings per group content. We discover all the group
// content that reference this group type.
$entity_info = entity_get_info();
$options = array();
foreach (og_get_all_group_content_bundle() as $entity_type => $bundles) {
if ($entity_type == 'user') {
// Adding terms to the user is probably not the use case, so skip
// it.
continue;
}
foreach ($bundles as $bundle => $bundle_label) {
foreach (og_get_group_audience_fields($entity_type, $bundle, FALSE) as $field_name => $field_label) {
$field = field_info_field($field_name);
if (empty($field['settings']['handler_settings']['target_bundles']) || in_array($group_bundle, $field['settings']['handler_settings']['target_bundles'])) {
$options[$entity_type][$bundle] = $entity_info[$entity_type]['label'] . ' - ' . $bundle_label;
}
}
}
}
if (!$options) {
$form['og_vocab'] = array(
'#markup' => t('There are no group contents referencing this group type.'),
);
return;
}
$form['og_vocab'] = array(
'#tree' => TRUE,
);
$vocabulary = $form['#vocabulary'];
$vid = !empty($vocabulary->vid) ? $vocabulary->vid : 0;
foreach ($options as $entity_type => $values) {
foreach ($values as $bundle => $name) {
// Load the OG-vocab record if exists, or a default one.
$form['og_vocab']["{$entity_type}:{$bundle}"] = array(
'#type' => 'fieldset',
'#title' => check_plain($name),
'#id' => drupal_clean_css_identifier("og-vocab-{$entity_type}-{$bundle}"),
);
// Load an existing OG-vocab, or get default values.
$og_vocab = og_vocab_load_og_vocab($vid, $entity_type, $bundle, NULL, TRUE);
$form['og_vocab']["{$entity_type}:{$bundle}"]['entity'] = array(
'#type' => 'value',
'#value' => $og_vocab,
);
// Get all the entity reference widget types, except OG-vocab's.
$form['og_vocab']["{$entity_type}:{$bundle}"]['status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#description' => t('Show term reference in @name add and edit form', array(
'@name' => $name,
)),
'#default_value' => empty($og_vocab->is_new),
);
module_load_include('inc', 'field_ui', 'field_ui.admin');
$widget_types = field_ui_widget_type_options('entityreference');
unset($widget_types['og_vocab_complex'], $widget_types['og_complex']);
$element_name = "og_vocab[{$entity_type}:{$bundle}][status]";
$states = array(
'#states' => array(
'visible' => array(
':input[name="' . $element_name . '"]' => array(
'checked' => TRUE,
),
),
),
);
$form['og_vocab']["{$entity_type}:{$bundle}"]['widget_type'] = array(
'#type' => 'select',
'#title' => t('Widget type'),
'#required' => TRUE,
'#options' => $widget_types,
'#default_value' => $og_vocab->settings['widget_type'],
'#description' => t('The type of form element you would like to present to the user when creating this field in the %type type.', array(
'%type' => $bundle_label,
)),
) + $states;
$form['og_vocab']["{$entity_type}:{$bundle}"]['required'] = array(
'#type' => 'checkbox',
'#title' => t('Required field'),
'#default_value' => $og_vocab->settings['required'],
) + $states;
$form['og_vocab']["{$entity_type}:{$bundle}"]['cardinality'] = array(
'#type' => 'select',
'#title' => t('Number of values'),
'#options' => array(
FIELD_CARDINALITY_UNLIMITED => t('Unlimited'),
) + drupal_map_assoc(range(1, 10)),
'#default_value' => $og_vocab->settings['cardinality'],
'#description' => t('Maximum number of values users can enter for this field.'),
) + $states;
// Add the field name to associate with. If there's just a single or
// it still doesn't exist we hide it, and select it as the default.
$og_vocab_fields = og_vocab_get_og_vocab_fields($entity_type, $bundle);
if (!empty($og_vocab_fields) && count($og_vocab_fields) > 1) {
$form['og_vocab']["{$entity_type}:{$bundle}"]['field_name'] = array(
'#title' => t('Field name'),
'#type' => 'select',
'#options' => $og_vocab_fields,
'#default_value' => $og_vocab->field_name,
);
}
else {
// Pass it as value.
$form['og_vocab']["{$entity_type}:{$bundle}"]['field_name'] = array(
'#type' => 'value',
'#value' => $og_vocab->field_name,
);
}
}
}
$form['#submit'][] = 'og_vocab_form_taxonomy_form_vocabulary_submit';
}