function dynamic_entity_reference_field_widget_form in Dynamic Entity Reference 7
Implements hook_field_widget_form().
File
- ./
dynamic_entity_reference.field.inc, line 95 - Contains field hooks.
Code
function dynamic_entity_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'dynamic_entity_reference_default':
$entity = $element['#entity'];
$item = isset($items[$delta]) ? $items[$delta] : NULL;
if ($item) {
$entities = entity_load($item['target_type'], array(
$item['target_id'],
));
if (!empty($entities)) {
$item['entity'] = entity_metadata_wrapper($item['target_type'], reset($entities));
}
else {
// Entity removed.
$item = NULL;
unset($items[$delta]);
}
}
global $user;
$element += array(
'#type' => 'textfield',
'#maxlength' => 1024,
'#default_value' => $item ? $item['entity']
->label() . ' (' . $item['entity']
->getIdentifier() . ')' : '',
'#autocomplete_path' => 'dynamic_entity_reference/autocomplete',
'#element_validate' => array(
'dynamic_entity_reference_field_widget_form_validate',
),
'#autocreate_uid' => isset($entity->uid) ? $entity->uid : $user->uid,
'#field_name' => $element['#field_name'],
);
$element['#title'] = t('Label');
// Select the target entity type.
$options = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
$options[$entity_type] = $entity_info['label'];
}
$entity_type_ids = $field['settings']['entity_type_ids'];
if ($field['settings']['exclude_entity_types']) {
$available = array_diff_key($options, $entity_type_ids ?: array());
}
else {
$available = array_intersect_key($options, $entity_type_ids ?: array());
}
$keys = array_keys($options);
$entity_type = array(
'#type' => 'select',
'#options' => $available,
'#title' => t('Entity type'),
'#default_value' => $item ? $item['target_type'] : reset($keys),
'#weight' => -50,
'#attributes' => array(
'class' => array(
'dynamic-entity-reference-entity-type',
),
),
);
return array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'container-inline',
),
),
'target_type' => $entity_type,
'target_id' => $element,
'#attached' => array(
'js' => array(
drupal_get_path('module', 'dynamic_entity_reference') . '/js/dynamic-entity-reference-widget.js' => array(
// This has to run after default autocomplete.
'weight' => 1000,
'scope' => 'footer',
),
),
),
);
break;
}
return array();
}