View source
<?php
module_load_include('inc', 'entity_translation_hierarchy', 'entity_translation_hierarchy.node');
function entity_translation_hierarchy_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'entity_translation_hierarchy') . '/views',
);
}
function entity_translation_hierarchy_field_attach_view_alter(&$output, $context) {
$entity = $context['entity'];
$entity_type = $context['entity_type'];
if (entity_translation_enabled($entity_type)) {
$handler = entity_translation_get_handler($entity_type, $entity);
$translations = $handler
->getTranslations();
$langcode = !empty($context['language']) ? $context['language'] : $GLOBALS['language_content']->language;
if (!empty($translations->data) && (!isset($translations->data[$langcode]) && !isset($translations->data[LANGUAGE_NONE]) || isset($translations->data[$langcode]) && !entity_translation_access($entity_type, $translations->data[$langcode])) && $langcode != LANGUAGE_NONE) {
if (!empty($translations->data[$langcode]['blocking'])) {
$output['#access'] = user_access('translate any entity') || user_access("translate {$entity_type} entities");
}
elseif (!($candidate = entity_translation_hierarchy_get_candidate($entity, $entity_type, $langcode))) {
$output['#entity'] = $entity;
$output['#entity_type'] = $entity_type;
$output['#view_mode'] = $context['view_mode'];
$output['#entity_translation_unavailable'] = theme('entity_translation_unavailable', array(
'element' => $output,
));
$output['#post_render']['entity_translation'] = 'entity_translation_unavailable';
}
}
}
}
function entity_translation_hierarchy_preprocess_entity_translation_overview(&$variables) {
$languages = array_values(language_hierarchy_language_list());
foreach ($languages as $index => $language) {
$variables['rows'][$index]['data'][0] .= theme('indentation', array(
'size' => $language->depth,
));
if ($current_entity = _entity_translation_hierarchy_load_current_entity()) {
list($entity_type, $entity) = $current_entity;
if ($entity && !empty($entity->translations->data[$language->language]['blocking'])) {
$variables['rows'][$index]['data'][3] = t('Blocking');
}
}
}
}
function entity_translation_hierarchy_form_alter(&$form, &$form_state, $form_id) {
if ($handler = entity_translation_entity_form_get_handler($form, $form_state)) {
$translations = $handler
->getTranslations();
$form_langcode = $handler
->getFormLanguage();
language_hierarchy_attach_language_selector($form);
if (isset($form['translation'])) {
$form['translation']['blocking'] = array(
'#type' => 'checkbox',
'#title' => t('Flag translation as blocking'),
'#description' => t("An blocking translation will not be visible and the parent translation won't be used instead. The difference between blocking translation and not published one is that a blocking translation won't load parent translation in place of the blocking one."),
'#default_value' => !empty($translations->data[$form_langcode]['blocking']),
'#weight' => -98,
);
$form['translation']['status']['#weight'] = -99;
$form['translation']['status']['#states'] = array(
'disabled' => array(
'#edit-translation-blocking' => array(
'checked' => TRUE,
),
),
'unchecked' => array(
'#edit-translation-blocking' => array(
'checked' => TRUE,
),
),
);
$form['translation']['#attached']['js'][] = drupal_get_path('module', 'entity_translation_hierarchy') . '/entity_translation_hierarchy.form.js';
}
}
}
function entity_translation_hierarchy_field_attach_submit($entity_type, $entity, $form, &$form_state) {
if ($handler = entity_translation_entity_form_get_handler($form, $form_state)) {
if (!$handler
->isNewEntity() && isset($form_state['values']['translation']['blocking'])) {
$form_language = $handler
->getFormLanguage();
$translations = $handler
->getTranslations();
$translations->data[$form_language]['blocking'] = $form_state['values']['translation']['blocking'];
}
}
}
function entity_translation_hierarchy_field_attach_update($entity_type, $entity) {
entity_translation_hierarchy_save_blocking_info($entity_type, $entity);
}
function entity_translation_hierarchy_field_attach_insert($entity_type, $entity) {
entity_translation_hierarchy_save_blocking_info($entity_type, $entity);
}
function entity_translation_hierarchy_save_blocking_info($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity) && ($handler = entity_translation_get_handler($entity_type, $entity, TRUE))) {
_entity_translation_hierarchy_save_blocking_info_table($entity_type, $entity, $handler, 'entity_translation');
if ($handler
->isRevisionable()) {
_entity_translation_hierarchy_save_blocking_info_table($entity_type, $entity, $handler, 'entity_translation_revision', TRUE);
}
}
}
function _entity_translation_hierarchy_save_blocking_info_table($entity_type, $entity, EntityTranslationHandlerInterface $handler, $table, $revision = FALSE) {
list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
$translations = $handler
->getTranslations();
$query = db_delete($table)
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id);
$langcode = $translations->original;
$hook = isset($translations->hook) ? $translations->hook : array();
if ($revision && $handler
->isRevisionable() && (empty($hook[$langcode]['hook']) || $hook[$langcode]['hook'] != 'delete')) {
$query
->condition('revision_id', $revision_id);
}
$query
->execute();
if (count($translations->data)) {
$columns = array(
'entity_type',
'entity_id',
'revision_id',
'language',
'source',
'uid',
'status',
'translate',
'created',
'changed',
'blocking',
);
$query = db_insert($table)
->fields($columns);
$overrides = array(
'entity_id' => $entity_id,
'entity_type' => $entity_type,
'revision_id' => $handler
->isRevisionable() ? $revision_id : $entity_id,
);
$defaults = array(
'source' => '',
'uid' => $GLOBALS['user']->uid,
'translate' => 0,
'status' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'blocking' => 0,
);
foreach ($translations->data as $langcode => $translation) {
$translation = $overrides + $translation + $defaults;
$query
->values($translation);
}
$query
->execute();
}
}
function entity_translation_hierarchy_get_candidate($entity, $entity_type, $langcode, $include_current = TRUE) {
if ($handler = entity_translation_get_handler($entity_type, $entity, TRUE)) {
if ($translations = $handler
->getTranslations()) {
$translation_candidates = $translations->data;
foreach ($translations->data as $translation_langcode => $translation) {
if (!entity_translation_access($entity_type, $translation)) {
unset($translation_candidates[$translation_langcode]);
}
}
if (!$include_current && isset($translation_candidates[$langcode])) {
unset($translation_candidates[$langcode]);
}
$fallback_candidates = array_keys(language_hierarchy_get_ancestors($langcode));
array_unshift($fallback_candidates, $langcode);
$translation_candidate_langcodes = array_keys($translation_candidates);
return current(array_intersect($fallback_candidates, $translation_candidate_langcodes));
}
}
return NULL;
}
function entity_translation_hierarchy_is_blocked($entity, $entity_type, $langcode) {
if (!entity_translation_enabled($entity_type, $entity)) {
return FALSE;
}
if ($handler = entity_translation_get_handler($entity_type, $entity, TRUE)) {
if ($translations = $handler
->getTranslations()) {
if (!isset($translations) || !$translations->original || empty($translations->data)) {
return FALSE;
}
$view_candidate = entity_translation_hierarchy_get_candidate($entity, $entity_type, $langcode);
if (!$view_candidate) {
return TRUE;
}
$is_blocking = !empty($translations->data[$view_candidate]['blocking']);
if ($is_blocking && (!user_access('translate any entity') && !user_access("translate {$entity_type} entities"))) {
return TRUE;
}
}
}
return FALSE;
}
function _entity_translation_hierarchy_load_current_entity() {
$menu_item = menu_get_item();
list($load_function) = array_values($menu_item['load_functions']);
$arg_position = array_search($load_function, $menu_item['load_functions']);
$entity_info = entity_get_info();
foreach ($entity_info as $current_entity_type => $info) {
if ($info['load hook'] == $load_function) {
$entity_type = $current_entity_type;
}
}
if (!empty($entity_type) && ($entity = menu_get_object($entity_type, $arg_position))) {
if (is_object($entity)) {
return array(
$entity_type,
$entity,
);
}
}
return array();
}