function entity_block_form in Entity Blocks 7
Returns the form for adding an entity block.
2 string references to 'entity_block_form'
- entity_block_add in ./
entity_block.module - Page callback for adding an entity block.
- entity_block_menu in ./
entity_block.module - Implements hook_menu().
File
- ./
entity_block.module, line 247 - Display entities (via view modes) as fieldable blocks.
Code
function entity_block_form($form, &$form_state, $entity) {
// Get entity types.
$entity_types = entity_block_get_entity_types();
// Attach fields.
field_attach_form('entity_block', $entity, $form, $form_state);
// Save entity to form.
$form['entity_block'] = array(
'#type' => 'value',
'#value' => $entity,
);
// Title field.
$form['title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => $entity->title,
);
// Target entity type field.
$entity_type_options = entity_block_get_entity_types_options();
$target_entity_type = !empty($form_state['values']['target_entity_type']) ? $form_state['values']['target_entity_type'] : $entity->target_entity_type;
$form['target_entity_type'] = array(
'#title' => t('Entity type'),
'#type' => 'select',
'#options' => $entity_type_options,
'#default_value' => !empty($entity->target_entity_type) ? $entity->target_entity_type : 'node',
'#required' => TRUE,
'#prefix' => '<div id="entity-block-target-entity-type">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'entity_block_target_bundle_form_callback',
'wrapper' => 'entity-block-target-bundle',
),
);
// Build bundle options.
$bundles = array();
foreach ($entity_types[$target_entity_type]['bundles'] as $bundle_name => $bundle_info) {
$bundles[$bundle_name] = $bundle_info['label'];
}
// Default target bundle.
$target_bundle = !empty($entity->target_bundle) ? $entity->target_bundle : key($bundles);
// Target bundle field.
$form['target_bundle'] = array(
'#type' => 'select',
'#title' => t('Bundle'),
'#prefix' => '<div id="entity-block-target-bundle">',
'#suffix' => '</div>',
'#options' => $bundles,
'#required' => TRUE,
'#default_value' => $target_bundle,
'#ajax' => array(
'callback' => 'entity_block_target_entity_id_form_callback',
'wrapper' => 'entity-block-target-entity-id',
),
);
// Target view mode.
if (!empty($target_entity_type)) {
// Build view mode options.
$view_modes = array();
$blacklisted_view_modes = entity_block_get_blacklisted_view_modes();
foreach ($entity_types[$target_entity_type]['view modes'] as $view_mode_name => $view_mode_info) {
if (!in_array($view_mode_name, $blacklisted_view_modes)) {
$view_modes[$view_mode_name] = $view_mode_info['label'];
}
}
// Default target view mode.
$target_view_mode = !empty($entity->target_view_mode) ? $entity->target_view_mode : key($view_modes);
$form['target_view_mode'] = array(
'#type' => 'select',
'#title' => t('View mode'),
'#prefix' => '<div id="entity-block-target-view-mode">',
'#suffix' => '</div>',
'#options' => $view_modes,
'#required' => TRUE,
'#default_value' => $target_view_mode,
);
$autocomplete_path = 'entityreference/autocomplete/entity_block/' . $target_entity_type . '/' . $target_bundle;
$target_entity = entity_load_single($entity->target_entity_type, $entity->target_entity_id);
$form['target_entity_id'] = array(
'#title' => 'Entity',
'#type' => 'textfield',
'#maxlength' => 1024,
'#default_value' => '',
'#prefix' => '<div id="entity-block-target-entity-id">',
'#suffix' => '</div>',
'#autocomplete_path' => $autocomplete_path,
'#default_value' => $entity->target_entity_id,
// @todo display the entity title using handler
'#required' => TRUE,
'#element_validate' => array(
'_entityreference_autocomplete_validate',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 500,
);
return $form;
}