function properties_template_field_attach_form in Dynamic properties 7
Implements hook_field_attach_form().
File
- properties_template/
properties_template.module, line 161 - This module implements the template functionality for properties.
Code
function properties_template_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
// Load fields attached to the entity.
$fields = field_info_instances($form['#entity_type'], $form['#bundle']);
// Loop over fields.
foreach ($fields as $field_name => $field) {
// If langcode is empty, get default.
if (empty($langcode)) {
$langcode = field_language($entity_type, $entity, $field_name);
}
$field_info = field_info_field_by_id($field['field_id']);
if ($field_info['type'] == 'properties' && isset($form_state[$field_name])) {
// Display template selection only when there are no properties.
if (empty($form_state[$field_name]['categories'])) {
// Bail out if permissions are missing.
if (!user_access('add properties templates')) {
return;
}
// Prepare options for select.
$templates = array(
t('Choose template...'),
);
foreach (properties_template_load_paging(100) as $template) {
$templates[$template->name] = $template->label;
}
$element['template'] = array(
'#type' => 'select',
'#title' => t('Select template'),
'#title_display' => 'invisible',
'#options' => $templates,
);
$id = 'properties-' . drupal_html_id($field_name) . '-wrapper';
$element['select_template'] = array(
'#type' => 'submit',
'#value' => t('Select template'),
'#limit_validation_errors' => array(
array(
$field_name,
$langcode,
),
),
'#submit' => array(
'properties_template_submit_select_template',
),
'#ajax' => array(
'callback' => 'properties_widget_update_js',
'wrapper' => $form[$field_name][$langcode]['#properties_table_id'],
'effect' => 'fade',
),
);
$form[$field_name][$langcode]['actions'] += $element;
}
else {
// Bail out if permissions are missing.
if (!user_access('administer properties templates')) {
return;
}
list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
$query = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'field_name' => $field_name,
);
// should create such a query object ?enity_type=node&entity_id=78
$link = 'admin/config/content/properties/templates/add/';
$element['template'] = array(
'#type' => 'link',
'#title' => t('Export as template'),
'#href' => $link,
'#options' => array(
'query' => $query,
),
);
$form[$field_name][$langcode]['actions'] += $element;
}
}
}
}