当前位置: 首页>>代码示例>>PHP>>正文


PHP EntityInterface::getTranslationLanguages方法代码示例

本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::getTranslationLanguages方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::getTranslationLanguages方法的具体用法?PHP EntityInterface::getTranslationLanguages怎么用?PHP EntityInterface::getTranslationLanguages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Drupal\Core\Entity\EntityInterface的用法示例。


在下文中一共展示了EntityInterface::getTranslationLanguages方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getTranslationFromContext

 /**
  * {@inheritdoc}
  */
 public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array())
 {
     $translation = $entity;
     if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
         if (empty($langcode)) {
             $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
             $entity->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]);
         }
         // Retrieve language fallback candidates to perform the entity language
         // negotiation, unless the current translation is already the desired one.
         if ($entity->language()->getId() != $langcode) {
             $context['data'] = $entity;
             $context += array('operation' => 'entity_view', 'langcode' => $langcode);
             $candidates = $this->languageManager->getFallbackCandidates($context);
             // Ensure the default language has the proper language code.
             $default_language = $entity->getUntranslated()->language();
             $candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT;
             // Return the most fitting entity translation.
             foreach ($candidates as $candidate) {
                 if ($entity->hasTranslation($candidate)) {
                     $translation = $entity->getTranslation($candidate);
                     break;
                 }
             }
         }
     }
     return $translation;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:31,代码来源:EntityRepository.php

示例2: getEntityTranslation

 /**
  * Returns the entity translation matching the configured row language.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity object the field value being processed is attached to.
  * @param \Drupal\views\ResultRow $row
  *   The result row the field value being processed belongs to.
  *
  * @return \Drupal\Core\Entity\FieldableEntityInterface
  *   The entity translation object for the specified row.
  */
 public function getEntityTranslation(EntityInterface $entity, ResultRow $row)
 {
     // We assume the same language should be used for all entity fields
     // belonging to a single row, even if they are attached to different entity
     // types. Below we apply language fallback to ensure a valid value is always
     // picked.
     $translation = $entity;
     if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
         $langcode = $this->getEntityTranslationRenderer()->getLangcode($row);
         $translation = $this->getEntityManager()->getTranslationFromContext($entity, $langcode);
     }
     return $translation;
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:24,代码来源:EntityTranslationRenderTrait.php

示例3: getFormSubmitSuffix

 /**
  * {@inheritdoc}
  */
 protected function getFormSubmitSuffix(EntityInterface $entity, $langcode)
 {
     if (!$entity->isNew() && $entity->isTranslatable()) {
         $translations = $entity->getTranslationLanguages();
         if ((count($translations) > 1 || !isset($translations[$langcode])) && ($field = $entity->getFieldDefinition('status'))) {
             return ' ' . ($field->isTranslatable() ? t('(this translation)') : t('(all translations)'));
         }
     }
     return '';
 }
开发者ID:alnutile,项目名称:drunatra,代码行数:13,代码来源:NodeTranslationUITest.php

示例4: entityFormAlter

 /**
  * {@inheritdoc}
  */
 public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity)
 {
     $form_object = $form_state->getFormObject();
     $form_langcode = $form_object->getFormLangcode($form_state);
     $entity_langcode = $entity->getUntranslated()->language()->getId();
     $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_object->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 = $this->languageManager->getLanguages();
     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_langcode) {
             $t_args = array('%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '!title' => $title);
             $title = empty($source_langcode) ? t('!title [%language translation]', $t_args) : t('Create %language translation of %title', $t_args);
         }
         $form['#title'] = $title;
     }
     // 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('#type' => 'details', '#title' => t('Source language: @language', array('@language' => $languages[$source_langcode]->getName())), '#tree' => TRUE, '#weight' => -100, '#multilingual' => TRUE, 'source' => array('#title' => t('Select source language'), '#title_display' => 'invisible', '#type' => 'select', '#default_value' => $source_langcode, '#options' => array()), 'submit' => array('#type' => 'submit', '#value' => t('Change'), '#submit' => array(array($this, 'entityFormSourceChange'))));
         foreach ($this->languageManager->getLanguages() as $language) {
             if (isset($translations[$language->getId()])) {
                 $form['source_langcode']['source']['#options'][$language->getId()] = $language->getName();
             }
         }
     }
     // Locate the language widget.
     $langcode_key = $this->entityType->getKey('langcode');
     if (isset($form[$langcode_key])) {
         $language_widget =& $form[$langcode_key];
     }
     // If we are editing the source entity, limit the list of languages so that
     // it is not possible to switch to a language for which a translation
     // already exists. Note that this will only work if the widget is structured
     // like \Drupal\Core\Field\Plugin\Field\FieldWidget\LanguageSelectWidget.
     if (isset($language_widget['widget'][0]['value']) && !$is_translation && $has_translations) {
         $language_select =& $language_widget['widget'][0]['value'];
         if ($language_select['#type'] == 'language_select') {
             $options = array();
             foreach ($this->languageManager->getLanguages() as $language) {
                 // Show the current language, and the languages for which no
                 // translation already exists.
                 if (empty($translations[$language->getId()]) || $language->getId() == $entity_langcode) {
                     $options[$language->getId()] = $language->getName();
                 }
             }
             $language_select['#options'] = $options;
         }
     }
     if ($is_translation) {
         if (isset($language_widget)) {
             $language_widget['widget']['#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;
                 }
             }
             $access = $this->getTranslationAccess($entity, 'delete')->isAllowed() || $entity->access('delete') && $this->entityType->hasLinkTemplate('delete-form');
             $form['actions']['delete_translation'] = array('#type' => 'submit', '#value' => t('Delete translation'), '#weight' => $weight, '#submit' => array(array($this, 'entityFormDeleteTranslation')), '#access' => $access);
         }
         // 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['content_translation'] = array('#type' => 'details', '#title' => t('Translation'), '#tree' => TRUE, '#weight' => 10, '#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update')->isAllowed(), '#multilingual' => TRUE);
         // A new translation is enabled by default.
         $metadata = $this->manager->getTranslationMetadata($entity);
         $status = $new_translation || $metadata->isPublished();
         // If there is only one published translation we cannot unpublish it,
         // since there would be nothing left to display.
         $enabled = TRUE;
         if ($status) {
             $published = 0;
             foreach ($entity->getTranslationLanguages() as $langcode => $language) {
                 $published += $this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished();
             }
             $enabled = $published > 1;
         }
         $description = $enabled ? t('An unpublished translation will not be visible without translation permissions.') : t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
         $form['content_translation']['status'] = array('#type' => 'checkbox', '#title' => t('This translation is published'), '#default_value' => $status, '#description' => $description, '#disabled' => !$enabled);
//.........这里部分代码省略.........
开发者ID:nsp15,项目名称:Drupal8,代码行数:101,代码来源:ContentTranslationHandler.php

示例5: saveFieldItems

 /**
  * Saves values of configurable fields for an entity.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity.
  * @param bool $update
  *   TRUE if the entity is being updated, FALSE if it is being inserted.
  */
 protected function saveFieldItems(EntityInterface $entity, $update = TRUE)
 {
     $vid = $entity->getRevisionId();
     $id = $entity->id();
     $bundle = $entity->bundle();
     $entity_type = $entity->getEntityTypeId();
     $default_langcode = $entity->getUntranslated()->language()->id;
     $translation_langcodes = array_keys($entity->getTranslationLanguages());
     if (!isset($vid)) {
         $vid = $id;
     }
     foreach ($this->entityManager->getFieldDefinitions($entity_type, $bundle) as $field_name => $field_definition) {
         $storage_definition = $field_definition->getFieldStorageDefinition();
         if (!$this->usesDedicatedTable($storage_definition)) {
             continue;
         }
         $table_name = static::_fieldTableName($storage_definition);
         $revision_name = static::_fieldRevisionTableName($storage_definition);
         // Delete and insert, rather than update, in case a value was added.
         if ($update) {
             // Only overwrite the field's base table if saving the default revision
             // of an entity.
             if ($entity->isDefaultRevision()) {
                 $this->database->delete($table_name)->condition('entity_id', $id)->execute();
             }
             $this->database->delete($revision_name)->condition('entity_id', $id)->condition('revision_id', $vid)->execute();
         }
         // Prepare the multi-insert query.
         $do_insert = FALSE;
         $columns = array('entity_id', 'revision_id', 'bundle', 'delta', 'langcode');
         foreach ($storage_definition->getColumns() as $column => $attributes) {
             $columns[] = static::_fieldColumnName($storage_definition, $column);
         }
         $query = $this->database->insert($table_name)->fields($columns);
         $revision_query = $this->database->insert($revision_name)->fields($columns);
         $langcodes = $field_definition->isTranslatable() ? $translation_langcodes : array($default_langcode);
         foreach ($langcodes as $langcode) {
             $delta_count = 0;
             $items = $entity->getTranslation($langcode)->get($field_name);
             $items->filterEmptyItems();
             foreach ($items as $delta => $item) {
                 // We now know we have someting to insert.
                 $do_insert = TRUE;
                 $record = array('entity_id' => $id, 'revision_id' => $vid, 'bundle' => $bundle, 'delta' => $delta, 'langcode' => $langcode);
                 foreach ($storage_definition->getColumns() as $column => $attributes) {
                     $column_name = static::_fieldColumnName($storage_definition, $column);
                     // Serialize the value if specified in the column schema.
                     $record[$column_name] = !empty($attributes['serialize']) ? serialize($item->{$column}) : $item->{$column};
                 }
                 $query->values($record);
                 $revision_query->values($record);
                 if ($storage_definition->getCardinality() != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && ++$delta_count == $storage_definition->getCardinality()) {
                     break;
                 }
             }
         }
         // Execute the query if we have values to insert.
         if ($do_insert) {
             // Only overwrite the field's base table if saving the default revision
             // of an entity.
             if ($entity->isDefaultRevision()) {
                 $query->execute();
             }
             $revision_query->execute();
         }
     }
 }
开发者ID:alnutile,项目名称:drunatra,代码行数:75,代码来源:ContentEntityDatabaseStorage.php

示例6: entityFormAlter

 /**
  * {@inheritdoc}
  */
 public function entityFormAlter(array &$form, array &$form_state, EntityInterface $entity)
 {
     $form_controller = content_translation_form_controller($form_state);
     $form_langcode = $form_controller->getFormLangcode($form_state);
     $entity_langcode = $entity->getUntranslated()->language()->id;
     $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_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);
         }
         $form['#title'] = $title;
     }
     // 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('#type' => 'details', '#title' => t('Source language: @language', array('@language' => $languages[$source_langcode]->name)), '#tree' => TRUE, '#weight' => -100, '#multilingual' => TRUE, 'source' => array('#title' => t('Select source language'), '#title_display' => 'invisible', '#type' => 'select', '#default_value' => $source_langcode, '#options' => array()), 'submit' => array('#type' => 'submit', '#value' => t('Change'), '#submit' => array(array($this, 'entityFormSourceChange'))));
         foreach (language_list(LanguageInterface::STATE_CONFIGURABLE) as $language) {
             if (isset($translations[$language->id])) {
                 $form['source_langcode']['source']['#options'][$language->id] = $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(LanguageInterface::STATE_CONFIGURABLE) as $language) {
             if (empty($translations[$language->id]) || $language->id == $entity_langcode) {
                 $form['langcode']['#options'][$language->id] = $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')), '#access' => $this->getTranslationAccess($entity, 'delete'));
         }
         // 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['content_translation'] = array('#type' => 'details', '#title' => t('Translation'), '#tree' => TRUE, '#weight' => 10, '#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update'), '#multilingual' => TRUE);
         // A new translation is enabled by default.
         $status = $new_translation || $entity->translation[$form_langcode]['status'];
         // If there is only one published translation we cannot unpublish it,
         // since there would be nothing left to display.
         $enabled = TRUE;
         if ($status) {
             // A new translation is not available in the translation metadata, hence
             // it should count as one more.
             $published = $new_translation;
             foreach ($entity->translation as $translation) {
                 $published += $translation['status'];
             }
             $enabled = $published > 1;
         }
         $description = $enabled ? t('An unpublished translation will not be visible without translation permissions.') : t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
         $form['content_translation']['status'] = array('#type' => 'checkbox', '#title' => t('This translation is published'), '#default_value' => $status, '#description' => $description, '#disabled' => !$enabled);
         $translate = !$new_translation && $entity->translation[$form_langcode]['outdated'];
         if (!$translate) {
             $form['content_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['content_translation']['outdated'] = 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.'));
         }
         // Default to the anonymous user.
         $name = '';
         if ($new_translation) {
             $name = \Drupal::currentUser()->getUsername();
         } elseif ($entity->translation[$form_langcode]['uid']) {
//.........这里部分代码省略.........
开发者ID:alnutile,项目名称:drunatra,代码行数:101,代码来源:ContentTranslationHandler.php


注:本文中的Drupal\Core\Entity\EntityInterface::getTranslationLanguages方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。