EntityTranslationController.php 16.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php

/**
 * @file
 * Definition of Drupal\translation_entity\EntityTranslationController.
 */

namespace Drupal\translation_entity;

use Drupal\Core\Entity\EntityInterface;

/**
 * Base class for entity translation controllers.
 */
class EntityTranslationController implements EntityTranslationControllerInterface {

  /**
   * The type of the entity being translated.
   *
   * @var string
   */
  protected $entityType;

  /**
   * The entity info of the entity being translated.
   *
   * @var array
   */
  protected $entityInfo;

  /**
   * Initializes an instance of the entity translation controller.
   *
   * @param string $entity_type
   *   The type of the entity being translated.
   * @param array $entity_info
   *   The info array of the given entity type.
   */
  public function __construct($entity_type, $entity_info) {
    $this->entityType = $entity_type;
    $this->entityInfo = $entity_info;
  }

  /**
   * Implements EntityTranslationControllerInterface::removeTranslation().
   */
  public function removeTranslation(EntityInterface $entity, $langcode) {
    $translations = $entity->getTranslationLanguages();
    // @todo Handle properties.
    // Remove field translations.
    foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) {
      $field_name = $instance['field_name'];
      $field = field_info_field($field_name);
      if ($field['translatable']) {
        $entity->{$field_name}[$langcode] = array();
      }
    }
  }

  /**
   * Implements EntityTranslationControllerInterface::retranslate().
   */
  public function retranslate(EntityInterface $entity, $langcode = NULL) {
    $updated_langcode = !empty($langcode) ? $langcode : $entity->language()->langcode;
    $translations = $entity->getTranslationLanguages();
    foreach ($translations as $langcode => $language) {
      $entity->retranslate[$langcode] = $langcode != $updated_langcode;
    }
  }

  /**
   * Implements EntityTranslationControllerInterface::getBasePath().
   */
  public function getBasePath(EntityInterface $entity) {
    return $this->getPathInstance($this->entityInfo['menu_base_path'], $entity->id());
  }

  /**
   * Implements EntityTranslationControllerInterface::getEditPath().
   */
  public function getEditPath(EntityInterface $entity) {
    return isset($this->entityInfo['menu_edit_path']) ? $this->getPathInstance($this->entityInfo['menu_edit_path'], $entity->id()) : FALSE;
  }

  /**
   * Implements EntityTranslationControllerInterface::getViewPath().
   */
  public function getViewPath(EntityInterface $entity) {
    return isset($this->entityInfo['menu_view_path']) ? $this->getPathInstance($this->entityInfo['menu_view_path'], $entity->id()) : FALSE;
  }

  /**
   * Implements EntityTranslationControllerInterface::getAccess().
   */
  public function getAccess(EntityInterface $entity, $op) {
    return TRUE;
  }

  /**
   * Implements EntityTranslationControllerInterface::getTranslationAccess().
   */
102 103 104 105 106 107 108 109 110 111 112
  public function getTranslationAccess(EntityInterface $entity, $op) {
    // @todo Move this logic into a translation access controller checking also
    //   the translation language and the given account.
    $info = $entity->entityInfo();
    $translate_permission = TRUE;
    // If no permission granularity is defined this entity type does not need an
    // explicit translate permission.
    if (!user_access('translate any entity') && !empty($info['permission_granularity'])) {
      $translate_permission = user_access($info['permission_granularity'] == 'bundle' ? "translate {$entity->bundle()} {$entity->entityType()}" : "translate {$entity->entityType()}");
    }
    return $translate_permission && user_access("$op entity translations");
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  }

  /**
   * Implements EntityTranslationControllerInterface::getSourceLanguage().
   */
  public function getSourceLangcode(array $form_state) {
    return isset($form_state['translation_entity']['source']) ? $form_state['translation_entity']['source']->langcode : FALSE;
  }

  /**
   * Implements EntityTranslationControllerInterface::entityFormAlter().
   */
  public function entityFormAlter(array &$form, array &$form_state, EntityInterface $entity) {
    $form_controller = translation_entity_form_controller($form_state);
    $form_langcode = $form_controller->getFormLangcode($form_state);
    $entity_langcode = $entity->language()->langcode;
    $source_langcode = $this->getSourceLangcode($form_state);

    $new_translation = !empty($source_langcode);
    $translations = $entity->getTranslationLanguages();
    if ($new_translation) {
      // Make sure a new translation does not appear as existing yet.
      unset($translations[$form_langcode]);
    }
    $is_translation = !$form_controller->isDefaultFormLangcode($form_state);
    $has_translations = count($translations) > 1;

    // Adjust page title to specify the current language being edited, if we
    // have at least one translation.
    $languages = language_list();
    if (isset($languages[$form_langcode]) && ($has_translations || $new_translation)) {
      $title = $this->entityFormTitle($entity);
      // When editing the original values display just the entity label.
      if ($form_langcode != $entity->language()->langcode) {
        $t_args = array('%language' => $languages[$form_langcode]->name, '%title' => $entity->label());
        $title = empty($source_langcode) ? $title . ' [' . t('%language translation', $t_args) . ']' : t('Create %language translation of %title', $t_args);
      }
      drupal_set_title($title, PASS_THROUGH);
    }

    // Display source language selector only if we are creating a new
    // translation and there are at least two translations available.
    if ($has_translations && $new_translation) {
      $form['source_langcode'] = array(
157
        '#type' => 'details',
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        '#title' => t('Source language: @language', array('@language' => $languages[$source_langcode]->name)),
        '#collapsed' => TRUE,
        '#tree' => TRUE,
        '#weight' => -100,
        '#multilingual' => TRUE,
        'source' => array(
          '#type' => 'select',
          '#default_value' => $source_langcode,
          '#options' => array(),
        ),
        'submit' => array(
          '#type' => 'submit',
          '#value' => t('Change'),
          '#submit' => array(array($this, 'entityFormSourceChange')),
        ),
      );
      foreach (language_list(LANGUAGE_CONFIGURABLE) as $language) {
        if (isset($translations[$language->langcode])) {
          $form['source_langcode']['source']['#options'][$language->langcode] = $language->name;
        }
      }
    }

    // Disable languages for existing translations, so it is not possible to
    // switch this node to some language which is already in the translation
    // set.
    $language_widget = isset($form['langcode']) && $form['langcode']['#type'] == 'language_select';
    if ($language_widget && $has_translations) {
      $form['langcode']['#options'] = array();
      foreach (language_list(LANGUAGE_CONFIGURABLE) as $language) {
        if (empty($translations[$language->langcode]) || $language->langcode == $entity_langcode) {
          $form['langcode']['#options'][$language->langcode] = $language->name;
        }
      }
    }

    if ($is_translation) {
      if ($language_widget) {
        $form['langcode']['#access'] = FALSE;
      }

      // Replace the delete button with the delete translation one.
      if (!$new_translation) {
        $weight = 100;
        foreach (array('delete', 'submit') as $key) {
          if (isset($form['actions'][$key]['weight'])) {
            $weight = $form['actions'][$key]['weight'];
            break;
          }
        }
        $form['actions']['delete_translation'] = array(
          '#type' => 'submit',
          '#value' => t('Delete translation'),
          '#weight' => $weight,
          '#submit' => array(array($this, 'entityFormDeleteTranslation')),
213
          '#access' => $this->getTranslationAccess($entity, 'delete'),
214 215 216 217 218 219 220 221 222 223 224
        );
      }

      // Always remove the delete button on translation forms.
      unset($form['actions']['delete']);
    }

    // We need to display the translation tab only when there is at least one
    // translation available or a new one is about to be created.
    if ($new_translation || $has_translations) {
      $form['translation'] = array(
225
        '#type' => 'details',
226 227 228 229
        '#title' => t('Translation'),
        '#collapsed' => TRUE,
        '#tree' => TRUE,
        '#weight' => 10,
230
        '#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update'),
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        '#multilingual' => TRUE,
      );

      $translate = !$new_translation && $entity->retranslate[$form_langcode];
      if (!$translate) {
        $form['translation']['retranslate'] = array(
          '#type' => 'checkbox',
          '#title' => t('Flag other translations as outdated'),
          '#default_value' => FALSE,
          '#description' => t('If you made a significant change, which means the other translations should be updated, you can flag all translations of this content as outdated. This will not change any other property of them, like whether they are published or not.'),
        );
      }
      else {
        $form['translation']['translate'] = array(
          '#type' => 'checkbox',
          '#title' => t('This translation needs to be updated'),
          '#default_value' => $translate,
          '#description' => t('When this option is checked, this translation needs to be updated. Uncheck when the translation is up to date again.'),
        );
      }

      if ($language_widget) {
253
        $form['langcode']['#multilingual'] = TRUE;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      }

      $form['#process'][] = array($this, 'entityFormSharedElements');
    }

    // Process the submitted values before they are stored.
    $form['#entity_builders'][] = array($this, 'entityFormEntityBuild');

    // Handle entity deletion.
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#submit'][] = array($this, 'entityFormDelete');
    }
  }

  /**
269
   * Process callback: determines which elements get clue in the form.
270 271 272
   *
   * @see \Drupal\translation_entity\EntityTranslationController::entityFormAlter()
   */
273
  public function entityFormSharedElements($element, $form_state, $form) {
274 275 276 277 278 279 280 281 282 283
    static $ignored_types;

    // @todo Find a more reliable way to determine if a form element concerns a
    //   multilingual value.
    if (!isset($ignored_types)) {
      $ignored_types = array_flip(array('actions', 'value', 'hidden', 'vertical_tabs', 'token'));
    }

    foreach (element_children($element) as $key) {
      if (!isset($element[$key]['#type'])) {
284
        $this->entityFormSharedElements($element[$key], $form_state, $form);
285 286 287 288 289 290 291 292
      }
      else {
        // Ignore non-widget form elements.
        if (isset($ignored_types[$element[$key]['#type']])) {
          continue;
        }
        // Elements are considered to be non multilingual by default.
        if (empty($element[$key]['#multilingual'])) {
293 294 295 296 297 298 299 300 301
          // If we are displaying a multilingual entity form we need to provide
          // translatability clues, otherwise the shared form elements should be
          // hidden.
          if (empty($form_state['translation_entity']['translation_form'])) {
            $this->addTranslatabilityClue($element[$key]);
          }
          else {
            $element[$key]['#access'] = FALSE;
          }
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        }
      }
    }

    return $element;
  }

  /**
   * Adds a clue about the form element translatability.
   *
   * If the given element does not have a #title attribute, the function is
   * recursively applied to child elements.
   *
   * @param array $element
   *   A form element array.
   */
  protected function addTranslatabilityClue(&$element) {
    static $suffix, $fapi_title_elements;

    // Elements which can have a #title attribute according to FAPI Reference.
    if (!isset($suffix)) {
      $suffix = ' <span class="translation-entity-all-languages">(' . t('all languages') . ')</span>';
324
      $fapi_title_elements = array_flip(array('checkbox', 'checkboxes', 'date', 'details', 'fieldset', 'file', 'item', 'password', 'password_confirm', 'radio', 'radios', 'select', 'text_format', 'textarea', 'textfield', 'weight'));
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    }

    // Update #title attribute for all elements that are allowed to have a
    // #title attribute according to the Form API Reference. The reason for this
    // check is because some elements have a #title attribute even though it is
    // not rendered, e.g. field containers.
    if (isset($element['#type']) && isset($fapi_title_elements[$element['#type']]) && isset($element['#title'])) {
      $element['#title'] .= $suffix;
    }
    // If the current element does not have a (valid) title, try child elements.
    elseif ($children = element_children($element)) {
      foreach ($children as $delta) {
        $this->addTranslatabilityClue($element[$delta], $suffix);
      }
    }
    // If there are no children, fall back to the current #title attribute if it
    // exists.
    elseif (isset($element['#title'])) {
      $element['#title'] .= $suffix;
    }
  }

  /**
   * Entity builder method.
   *
   * @param string $entity_type
   *   The type of the entity.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity whose form is being built.
   *
   * @see \Drupal\translation_entity\EntityTranslationController::entityFormAlter()
   */
  public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, array &$form_state) {
    $form_controller = translation_entity_form_controller($form_state);
    $form_langcode = $form_controller->getFormLangcode($form_state);
    $source_langcode = $this->getSourceLangcode($form_state);

    if ($source_langcode) {
      // @todo Use the entity setter when all entities support multilingual
      // properties.
      $entity->source[$form_langcode] = $source_langcode;
    }

    // Ensure every key has at least a default value. Subclasses may provide
    // entity-specific values to alter them.
    $values = isset($form_state['values']['translation']) ? $form_state['values']['translation'] : array();
    $entity->retranslate[$form_langcode] = isset($values['translate']) && $values['translate'];

    if (!empty($values['retranslate'])) {
      $this->retranslate($entity, $form_langcode);
    }
  }

  /**
   * Form submission handler for EntityTranslationController::entityFormAlter().
   *
   * Takes care of the source language change.
   */
  public function entityFormSourceChange($form, &$form_state) {
    $form_controller = translation_entity_form_controller($form_state);
    $entity = $form_controller->getEntity($form_state);
    $source = $form_state['values']['source_langcode']['source'];
    $path = $this->getBasePath($entity) . '/translations/add/' . $source . '/' . $form_controller->getFormLangcode($form_state);
    $form_state['redirect'] = array('path' => $path);
    $languages = language_list();
    drupal_set_message(t('Source language set to: %language', array('%language' => $languages[$source]->name)));
  }

  /**
   * Form submission handler for EntityTranslationController::entityFormAlter().
   *
   * Takes care of entity deletion.
   */
  function entityFormDelete($form, &$form_state) {
    $form_controller = translation_entity_form_controller($form_state);
    $entity = $form_controller->getEntity($form_state);
    if (count($entity->getTranslationLanguages()) > 1) {
      drupal_set_message(t('This will delete all the translations of %label.', array('%label' => $entity->label())), 'warning');
    }
  }

  /**
   * Form submission handler for EntityTranslationController::entityFormAlter().
   *
   * Takes care of entity translation deletion.
   */
  function entityFormDeleteTranslation($form, &$form_state) {
    $form_controller = translation_entity_form_controller($form_state);
    $entity = $form_controller->getEntity($form_state);
    $base_path = $this->getBasePath($entity);
    $form_langcode = $form_controller->getFormLangcode($form_state);
    $form_state['redirect'] = $base_path . '/translations/delete/' . $form_langcode;
  }

  /**
   * Returns the title to be used for the entity form page.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity whose form is being altered.
   */
  protected function entityFormTitle(EntityInterface $entity) {
    return $entity->label();
  }

  /**
   * Returns an instance of the given path.
   *
   * @param $path
   *   An internal path containing the entity id wildcard.
   *
   * @return string
   *   The instantiated path.
   */
  protected function getPathInstance($path, $entity_id) {
    $wildcard = $this->entityInfo['menu_path_wildcard'];
    return str_replace($wildcard, $entity_id, $path);
  }
}