View source
<?php
define('AET_INSERT_FIELD_AJAX_PATH', 'aet_insert/ajax/field');
define('AET_INSERT_UUID', module_exists('uuid'));
function aet_insert_menu() {
$items = array();
$items[AET_INSERT_FIELD_AJAX_PATH] = array(
'page callback' => 'aet_insert_field_ajax',
'access callback' => 'aet_insert_ajax_access',
'access arguments' => array(
'ajax',
array(
'field',
),
),
'file' => 'aet_insert.ajax.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function aet_insert_permission() {
return array(
'administer AET Insert' => array(
'title' => t('Administer AET Insert'),
'description' => t('Administration tasks for AET Insert.'),
'restrict access' => TRUE,
),
'use AET Insert' => array(
'title' => t('Use AET Insert'),
'description' => t('Using the AET Insert field.'),
),
);
}
function aet_insert_ajax_access($type, $args) {
return user_access('use AET Insert');
}
function aet_insert_init() {
drupal_add_js(array(
'aet_insert' => array(
'ajax_path' => base_path() . AET_INSERT_FIELD_AJAX_PATH,
),
), 'setting');
}
function aet_insert_element_info() {
$types = array();
$types['aet_insert_field'] = array(
'#input' => TRUE,
'#pre_render' => array(
'aet_insert_field_pre_render',
),
);
return $types;
}
function aet_insert_theme($existing, $type, $theme, $path) {
$themes = array();
$themes['aet_insert_field'] = array(
'render element' => 'element',
'file' => 'aet_insert.theme',
);
return $themes;
}
function aet_insert_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
if (user_access('administer AET Insert')) {
$form['group_aet_insert'] = array(
'#title' => t('AET Insert Settings'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$entity_type = $form['#instance']['entity_type'];
$field_name = $form['#instance']['field_name'];
$field_identifier = $entity_type . '_' . $field_name;
$query = db_select('aet_insert', 'ai')
->fields('ai', array(
'entity_type',
'field_name',
))
->condition('ai.entity_type', $entity_type, '=')
->condition('ai.field_name', $field_name, '=');
$results = $query
->execute();
$exists = FALSE;
foreach ($results as $result) {
$exists = TRUE;
}
$form['group_aet_insert']['aet_insert_use_' . $field_identifier] = array(
'#type' => 'checkbox',
'#title' => t('Use AET Insert with this field.'),
'#default_value' => $exists,
'#description' => t('Check this box if you want that the AET Insert field will be used' . ' with this one.'),
);
$form['#submit'][] = 'aet_insert_form_field_ui_field_edit_form_submit';
}
}
function aet_insert_form_alter(&$form, &$form_state, $form_id) {
if (user_access('use AET Insert') && isset($form['#entity']) && !empty($form_state['field'])) {
$entity = $form['#entity'];
$entity_type = $form['#entity_type'];
$entity_info = entity_get_info($entity_type);
$id_key = $entity_info['entity keys']['id'];
$id = isset($entity->{$id_key}) ? $entity->{$id_key} : FALSE;
$uuid = AET_INSERT_UUID && isset($entity->uuid) ? $entity->uuid : FALSE;
$bundle_key = !empty($entity_info['budle keys']) ? $entity_info['budle keys']['bundle'] : FALSE;
$bundle = $bundle_key && isset($entity->{$bundle_key}) ? $entity->{$bundle_key} : FALSE;
$query = db_select('aet_insert', 'ai')
->fields('ai', array(
'entity_type',
'field_name',
))
->condition('ai.entity_type', $entity_type, '=');
$results = $query
->execute();
$field_names = array();
foreach ($results as $result) {
$field_names[] = $result->field_name;
}
foreach ($field_names as $field_name) {
if (isset($form_state['field'][$field_name]) && !empty($form[$field_name])) {
$lang = $form[$field_name]['#language'];
for ($delta = 0; $delta < count($form[$field_name][$lang]) && isset($form[$field_name][$lang][$delta]); $delta++) {
$form[$field_name][$lang][$delta]['aet_insert_field'] = array(
'#type' => 'aet_insert_field',
'#theme' => 'aet_insert_field',
'#weight' => 999,
'#target' => 'edit-' . str_replace("_", "-", $field_name) . '-' . $lang . '-' . $delta . '-value',
'#entity_type' => $entity_type,
"#entity_key" => $id_key,
"#{$id_key}" => $id,
'#bundle' => $bundle,
);
if ($uuid) {
$form[$field_name][$lang][$delta]['aet_insert_field']['#entity_uuid'] = $uuid;
}
}
}
}
}
}
function aet_insert_field_pre_render($element) {
module_load_include('inc', 'aet_insert', 'aet_insert.fields');
$element = _aet_insert_field_pre_render($element);
$element += module_invoke_all('hook_aet_insert_field_pre_render', $element);
return $element;
}
function aet_insert_form_field_ui_field_edit_form_submit($form, &$form_state) {
$entity_type = $form['#instance']['entity_type'];
$field_name = $form['#instance']['field_name'];
$field_identifier = $entity_type . '_' . $field_name;
if (isset($form_state['values']['aet_insert_use_' . $field_identifier])) {
db_insert('aet_insert')
->fields(array(
'entity_type' => $entity_type,
'field_name' => $field_name,
))
->execute();
}
}
function _aet_insert_is_valid($id) {
return is_numeric($id) || AET_INSERT_UUID && uuid_is_valid($id);
}