Skip to content
Snippets Groups Projects
Commit 38fdf672 authored by catch's avatar catch
Browse files

Revert "Issue #2019055 by plach, fago, kfritsche: Switch from field-level...

Revert "Issue #2019055 by plach, fago, kfritsche: Switch from field-level language fallback to entity-level language fallback."

This reverts commit 46942cd3.
parent 46942cd3
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Showing
with 155 additions and 281 deletions
...@@ -218,7 +218,7 @@ services: ...@@ -218,7 +218,7 @@ services:
arguments: ['@event_dispatcher', '@service_container', '@controller_resolver'] arguments: ['@event_dispatcher', '@service_container', '@controller_resolver']
language_manager: language_manager:
class: Drupal\Core\Language\LanguageManager class: Drupal\Core\Language\LanguageManager
arguments: ['@state', '@module_handler'] arguments: ['@state']
string_translator.custom_strings: string_translator.custom_strings:
class: Drupal\Core\StringTranslation\Translator\CustomStrings class: Drupal\Core\StringTranslation\Translator\CustomStrings
arguments: ['@settings'] arguments: ['@settings']
......
...@@ -545,6 +545,30 @@ function language_url_split_prefix($path, $languages) { ...@@ -545,6 +545,30 @@ function language_url_split_prefix($path, $languages) {
return array(FALSE, $path); return array(FALSE, $path);
} }
/**
* Returns the possible fallback languages ordered by language weight.
*
* @param
* (optional) The language type. Defaults to Language::TYPE_CONTENT.
*
* @return
* An array of language codes.
*/
function language_fallback_get_candidates($type = Language::TYPE_CONTENT) {
$fallback_candidates = &drupal_static(__FUNCTION__);
if (!isset($fallback_candidates)) {
// Get languages ordered by weight, add Language::LANGCODE_NOT_SPECIFIED at the end.
$fallback_candidates = array_keys(language_list());
$fallback_candidates[] = Language::LANGCODE_NOT_SPECIFIED;
// Let other modules hook in and add/change candidates.
drupal_alter('language_fallback_candidates', $fallback_candidates);
}
return $fallback_candidates;
}
/** /**
* @} End of "language_negotiation" * @} End of "language_negotiation"
*/ */
...@@ -423,12 +423,6 @@ protected function getTranslatedField($property_name, $langcode) { ...@@ -423,12 +423,6 @@ protected function getTranslatedField($property_name, $langcode) {
*/ */
public function set($property_name, $value, $notify = TRUE) { public function set($property_name, $value, $notify = TRUE) {
$this->get($property_name)->setValue($value, FALSE); $this->get($property_name)->setValue($value, FALSE);
if ($property_name == 'langcode') {
// Avoid using unset as this unnecessarily triggers magic methods later
// on.
$this->language = NULL;
}
} }
/** /**
...@@ -664,7 +658,6 @@ protected function initializeTranslation($langcode) { ...@@ -664,7 +658,6 @@ protected function initializeTranslation($langcode) {
$translation->values = &$this->values; $translation->values = &$this->values;
$translation->fields = &$this->fields; $translation->fields = &$this->fields;
$translation->translations = &$this->translations; $translation->translations = &$this->translations;
$translation->enforceIsNew = &$this->enforceIsNew;
$translation->translationInitialize = FALSE; $translation->translationInitialize = FALSE;
return $translation; return $translation;
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\Core\Entity; namespace Drupal\Core\Entity;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Entity form controller variant for content entity types. * Entity form controller variant for content entity types.
...@@ -17,32 +16,6 @@ ...@@ -17,32 +16,6 @@
*/ */
class ContentEntityFormController extends EntityFormController { class ContentEntityFormController extends EntityFormController {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a ContentEntityFormController object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -65,7 +38,6 @@ public function form(array $form, array &$form_state) { ...@@ -65,7 +38,6 @@ public function form(array $form, array &$form_state) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, array &$form_state) { public function validate(array $form, array &$form_state) {
$this->updateFormLangcode($form_state);
$entity = $this->buildEntity($form, $form_state); $entity = $this->buildEntity($form, $form_state);
$entity_type = $entity->entityType(); $entity_type = $entity->entityType();
$entity_langcode = $entity->language()->id; $entity_langcode = $entity->language()->id;
...@@ -101,22 +73,53 @@ public function validate(array $form, array &$form_state) { ...@@ -101,22 +73,53 @@ public function validate(array $form, array &$form_state) {
protected function init(array &$form_state) { protected function init(array &$form_state) {
// Ensure we act on the translation object corresponding to the current form // Ensure we act on the translation object corresponding to the current form
// language. // language.
$langcode = $this->getFormLangcode($form_state); $this->entity = $this->getTranslatedEntity($form_state);
$this->entity = $this->entity->getTranslation($langcode);
parent::init($form_state); parent::init($form_state);
} }
/**
* Returns the translation object corresponding to the form language.
*
* @param array $form_state
* A keyed array containing the current state of the form.
*/
protected function getTranslatedEntity(array $form_state) {
$langcode = $this->getFormLangcode($form_state);
$translation = $this->entity->getTranslation($langcode);
// Ensure that the entity object is a BC entity if the original one is.
return $this->entity instanceof EntityBCDecorator ? $translation->getBCEntity() : $translation;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormLangcode(array &$form_state) { public function getFormLangcode(array $form_state) {
if (empty($form_state['langcode'])) { $entity = $this->entity;
// Imply a 'view' operation to ensure users edit entities in the same if (!empty($form_state['langcode'])) {
// language they are displayed. This allows to keep contextual editing $langcode = $form_state['langcode'];
// working also for multilingual entities. }
$form_state['langcode'] = $this->entityManager->getTranslationFromContext($this->entity)->language()->id; else {
// If no form langcode was provided we default to the current content
// language and inspect existing translations to find a valid fallback,
// if any.
$translations = $entity->getTranslationLanguages();
$languageManager = \Drupal::languageManager();
$langcode = $languageManager->getLanguage(Language::TYPE_CONTENT)->id;
$fallback = $languageManager->isMultilingual() ? language_fallback_get_candidates() : array();
while (!empty($langcode) && !isset($translations[$langcode])) {
$langcode = array_shift($fallback);
}
}
// If the site is not multilingual or no translation for the given form
// language is available, fall back to the entity language.
if (!empty($langcode)) {
return $langcode;
}
else {
// If the entity is translatable, return the original language.
return $entity->getUntranslated()->language()->id;
} }
return $form_state['langcode'];
} }
/** /**
...@@ -133,8 +136,8 @@ public function buildEntity(array $form, array &$form_state) { ...@@ -133,8 +136,8 @@ public function buildEntity(array $form, array &$form_state) {
$entity = clone $this->entity; $entity = clone $this->entity;
$entity_type = $entity->entityType(); $entity_type = $entity->entityType();
$info = entity_get_info($entity_type); $info = entity_get_info($entity_type);
// @todo Exploit the Field API to process the submitted entity fields.
// @todo Exploit the Entity Field API to process the submitted field values.
// Copy top-level form values that are entity fields but not handled by // Copy top-level form values that are entity fields but not handled by
// field API without changing existing entity fields that are not being // field API without changing existing entity fields that are not being
// edited by this form. Values of fields handled by field API are copied // edited by this form. Values of fields handled by field API are copied
...@@ -160,5 +163,4 @@ public function buildEntity(array $form, array &$form_state) { ...@@ -160,5 +163,4 @@ public function buildEntity(array $form, array &$form_state) {
} }
return $entity; return $entity;
} }
} }
...@@ -269,7 +269,6 @@ protected function actions(array $form, array &$form_state) { ...@@ -269,7 +269,6 @@ protected function actions(array $form, array &$form_state) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, array &$form_state) { public function validate(array $form, array &$form_state) {
$this->updateFormLangcode($form_state);
// @todo Remove this. // @todo Remove this.
// Execute legacy global validation handlers. // Execute legacy global validation handlers.
unset($form_state['validate_handlers']); unset($form_state['validate_handlers']);
...@@ -293,6 +292,8 @@ public function validate(array $form, array &$form_state) { ...@@ -293,6 +292,8 @@ public function validate(array $form, array &$form_state) {
public function submit(array $form, array &$form_state) { public function submit(array $form, array &$form_state) {
// Remove button and internal Form API values from submitted values. // Remove button and internal Form API values from submitted values.
form_state_values_clean($form_state); form_state_values_clean($form_state);
$this->updateFormLangcode($form_state);
$this->entity = $this->buildEntity($form, $form_state); $this->entity = $this->buildEntity($form, $form_state);
return $this->entity; return $this->entity;
} }
...@@ -324,7 +325,7 @@ public function delete(array $form, array &$form_state) { ...@@ -324,7 +325,7 @@ public function delete(array $form, array &$form_state) {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormLangcode(array &$form_state) { public function getFormLangcode(array $form_state) {
return $this->entity->language()->id; return $this->entity->language()->id;
} }
......
...@@ -26,7 +26,7 @@ interface EntityFormControllerInterface extends BaseFormIdInterface { ...@@ -26,7 +26,7 @@ interface EntityFormControllerInterface extends BaseFormIdInterface {
* @return string * @return string
* The form language code. * The form language code.
*/ */
public function getFormLangcode(array &$form_state); public function getFormLangcode(array $form_state);
/** /**
* Checks whether the current form language matches the entity one. * Checks whether the current form language matches the entity one.
......
...@@ -91,7 +91,7 @@ public function bundle(); ...@@ -91,7 +91,7 @@ public function bundle();
* *
* @param $langcode * @param $langcode
* (optional) The language code of the language that should be used for * (optional) The language code of the language that should be used for
* getting the label. If set to NULL, the entity's active language is * getting the label. If set to NULL, the entity's default language is
* used. * used.
* *
* @return * @return
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
...@@ -18,8 +17,8 @@ ...@@ -18,8 +17,8 @@
use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\InfoHookDecorator; use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -457,37 +456,4 @@ public function getEntityTypeLabels() { ...@@ -457,37 +456,4 @@ public function getEntityTypeLabels() {
return $options; return $options;
} }
/**
* {@inheritdoc}
*/
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) {
$translation = $entity;
if ($entity instanceof TranslatableInterface) {
if (empty($langcode)) {
$langcode = $this->languageManager->getLanguage(Language::TYPE_CONTENT)->id;
}
// Retrieve language fallback candidates to perform the entity language
// negotiation.
$context['data'] = $entity;
$context += array('operation' => 'entity_view');
$candidates = $this->languageManager->getFallbackCandidates($langcode, $context);
// Ensure the default language has the proper language code.
$default_language = $entity->getUntranslated()->language();
$candidates[$default_language->id] = Language::LANGCODE_DEFAULT;
// Return the most fitting entity translation.
foreach ($candidates as $candidate) {
if ($entity->hasTranslation($candidate)) {
$translation = $entity->getTranslation($candidate);
break;
}
}
}
return $translation;
}
} }
...@@ -244,27 +244,4 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $ ...@@ -244,27 +244,4 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $
*/ */
public function getBundleInfo($entity_type); public function getBundleInfo($entity_type);
/**
* Returns the entity translation to be used in the given context.
*
* This will check whether a translation for the desired language is available
* and if not, it will fall back to the most appropriate translation based on
* the provided context.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose translation will be returned.
* @param string $langcode
* (optional) The language of the current context. Defaults to the current
* content language.
* @param array $context
* (optional) An associative array of arbitrary data that can be useful to
* determine the proper fallback sequence.
*
* @return \Drupal\Core\Entity\EntityInterface
* An entity object for the translated data.
*
* @see \Drupal\Core\Language\LanguageManager::getFallbackCandidates()
*/
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array());
} }
...@@ -7,15 +7,13 @@ ...@@ -7,15 +7,13 @@
namespace Drupal\Core\Entity; namespace Drupal\Core\Entity;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Language\Language;
use Drupal\entity\Entity\EntityDisplay; use Drupal\entity\Entity\EntityDisplay;
use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Language\Language;
/** /**
* Base class for entity view controllers. * Base class for entity view controllers.
*/ */
class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderInterface { class EntityViewBuilder implements EntityViewBuilderInterface {
/** /**
* The type of entities for which this controller is instantiated. * The type of entities for which this controller is instantiated.
...@@ -33,13 +31,6 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI ...@@ -33,13 +31,6 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI
*/ */
protected $entityInfo; protected $entityInfo;
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/** /**
* An array of view mode info for the type of entities for which this * An array of view mode info for the type of entities for which this
* controller is instantiated. * controller is instantiated.
...@@ -58,30 +49,12 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI ...@@ -58,30 +49,12 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI
*/ */
protected $cacheBin = 'cache'; protected $cacheBin = 'cache';
/** public function __construct($entity_type) {
* Constructs a new EntityViewBuilder.
*
* @param string $entity_type
* The entity type.
* @param array $entity_info
* The entity information array.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
*/
public function __construct($entity_type, array $entity_info, EntityManager $entity_manager) {
$this->entityType = $entity_type; $this->entityType = $entity_type;
$this->entityInfo = $entity_info; $this->entityInfo = entity_get_info($entity_type);
$this->entityManager = $entity_manager;
$this->viewModesInfo = entity_get_view_modes($entity_type); $this->viewModesInfo = entity_get_view_modes($entity_type);
} }
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static($entity_type, $entity_info, $container->get('entity.manager'));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -194,14 +167,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la ...@@ -194,14 +167,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
$view_modes = array(); $view_modes = array();
$displays = array(); $displays = array();
$context = array('langcode' => $langcode); $context = array('langcode' => $langcode);
foreach ($entities as $key => $entity) { foreach ($entities as $entity) {
$bundle = $entity->bundle(); $bundle = $entity->bundle();
// Ensure that from now on we are dealing with the proper translation
// object.
$entity = $this->entityManager->getTranslationFromContext($entity, $langcode);
$entities[$key] = $entity;
// Allow modules to change the view mode. // Allow modules to change the view mode.
$entity_view_mode = $view_mode; $entity_view_mode = $view_mode;
drupal_alter('entity_view_mode', $entity_view_mode, $entity, $context); drupal_alter('entity_view_mode', $entity_view_mode, $entity, $context);
......
...@@ -153,7 +153,7 @@ public function extend($obj) { ...@@ -153,7 +153,7 @@ public function extend($obj) {
* @param array $languages * @param array $languages
* The array of language objects keyed by langcode. * The array of language objects keyed by langcode.
*/ */
public static function sort(&$languages) { public static function sort($languages) {
uasort($languages, function ($a, $b) { uasort($languages, function ($a, $b) {
$a_weight = isset($a->weight) ? $a->weight : 0; $a_weight = isset($a->weight) ? $a->weight : 0;
$b_weight = isset($b->weight) ? $b->weight : 0; $b_weight = isset($b->weight) ? $b->weight : 0;
......
...@@ -7,10 +7,8 @@ ...@@ -7,10 +7,8 @@
namespace Drupal\Core\Language; namespace Drupal\Core\Language;
use Drupal\Component\Utility\MapArray;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
/** /**
* Class responsible for initializing each language type. * Class responsible for initializing each language type.
...@@ -31,13 +29,6 @@ class LanguageManager { ...@@ -31,13 +29,6 @@ class LanguageManager {
*/ */
protected $state = NULL; protected $state = NULL;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/** /**
* An array of language objects keyed by language type. * An array of language objects keyed by language type.
* *
...@@ -66,13 +57,10 @@ class LanguageManager { ...@@ -66,13 +57,10 @@ class LanguageManager {
* Constructs an LanguageManager object. * Constructs an LanguageManager object.
* *
* @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state
* (optional) The state keyvalue store. Defaults to NULL. * The state keyvalue store.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* (optional) The module handler service. Defaults to NULL.
*/ */
public function __construct(KeyValueStoreInterface $state = NULL, ModuleHandlerInterface $module_handler = NULL) { public function __construct(KeyValueStoreInterface $state = NULL) {
$this->state = $state; $this->state = $state;
$this->moduleHandler = $module_handler;
} }
/** /**
...@@ -173,53 +161,6 @@ public function isMultilingual() { ...@@ -173,53 +161,6 @@ public function isMultilingual() {
return ($this->state->get('language_count') ?: 1) > 1; return ($this->state->get('language_count') ?: 1) > 1;
} }
/**
* Returns the language fallback candidates for a given context.
*
* @param string $langcode
* (optional) The language of the current context. Defaults to NULL.
* @param array $context
* (optional) An associative array of data that can be useful to determine
* the fallback sequence. The following keys are used in core:
* - langcode: The desired language.
* - operation: The name of the operation indicating the context where
* language fallback is being applied, e.g. 'entity_view'.
* - data: An arbitrary data structure that makes sense in the provided
* context, e.g. an entity.
*
* @return array
* An array of language codes sorted by priority: first values should be
* tried first.
*/
public function getFallbackCandidates($langcode = NULL, array $context = array()) {
if ($this->isMultilingual()) {
// Get languages ordered by weight, add Language::LANGCODE_NOT_SPECIFIED at
// the end.
$candidates = array_keys(language_list());
$candidates[] = Language::LANGCODE_NOT_SPECIFIED;
$candidates = MapArray::copyValuesToKeys($candidates);
// The first candidate should always be the desired language if specified.
if (!empty($langcode)) {
$candidates = array($langcode => $langcode) + $candidates;
}
// Let other modules hook in and add/change candidates.
$type = 'language_fallback_candidates';
$types = array();
if (!empty($context['operation'])) {
$types[] = $type . '_' . $context['operation'];
}
$types[] = $type;
$this->moduleHandler->alter($types, $candidates, $context);
}
else {
$candidates = array(Language::LANGCODE_DEFAULT);
}
return $candidates;
}
/** /**
* Returns an array of the available language types. * Returns an array of the available language types.
* *
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
namespace Drupal\Core\TypedData; namespace Drupal\Core\TypedData;
use Drupal\Core\Language\LanguageManager;
/** /**
* Interface for translatable data. * Interface for translatable data.
*/ */
...@@ -37,9 +35,10 @@ public function getTranslationLanguages($include_default = TRUE); ...@@ -37,9 +35,10 @@ public function getTranslationLanguages($include_default = TRUE);
/** /**
* Gets a translation of the data. * Gets a translation of the data.
* *
* The returned translation has to be of the same type than this typed data * The returned translation has to be implement the same typed data interfaces
* object. If the specified translation does not exist, a new one will be * as this typed data object, excluding the TranslatableInterface. E.g., if
* instantiated. * this typed data object implements the ComplexDataInterface and
* AccessibleInterface, the translation object has to implement both as well.
* *
* @param $langcode * @param $langcode
* The language code of the translation to get or Language::LANGCODE_DEFAULT * The language code of the translation to get or Language::LANGCODE_DEFAULT
...@@ -50,6 +49,7 @@ public function getTranslationLanguages($include_default = TRUE); ...@@ -50,6 +49,7 @@ public function getTranslationLanguages($include_default = TRUE);
*/ */
public function getTranslation($langcode); public function getTranslation($langcode);
/** /**
* Returns the translatable object referring to the original language. * Returns the translatable object referring to the original language.
* *
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Entity\ContentEntityFormController; use Drupal\Core\Entity\ContentEntityFormController;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\aggregator\CategoryStorageControllerInterface; use Drupal\aggregator\CategoryStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -19,6 +19,13 @@ ...@@ -19,6 +19,13 @@
*/ */
class FeedFormController extends ContentEntityFormController { class FeedFormController extends ContentEntityFormController {
/**
* The feed storage.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $feedStorageController;
/** /**
* The category storage controller. * The category storage controller.
* *
...@@ -29,13 +36,13 @@ class FeedFormController extends ContentEntityFormController { ...@@ -29,13 +36,13 @@ class FeedFormController extends ContentEntityFormController {
/** /**
* Constructs a FeedForm object. * Constructs a FeedForm object.
* *
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * @param \Drupal\Core\Entity\EntityStorageControllerInterface $feed_storage
* The entity manager. * The feed storage.
* @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller
* The category storage controller. * The category storage controller.
*/ */
public function __construct(EntityManagerInterface $entity_manager, CategoryStorageControllerInterface $category_storage_controller) { public function __construct(EntityStorageControllerInterface $feed_storage, CategoryStorageControllerInterface $category_storage_controller) {
parent::__construct($entity_manager); $this->feedStorageController = $feed_storage;
$this->categoryStorageController = $category_storage_controller; $this->categoryStorageController = $category_storage_controller;
} }
...@@ -44,7 +51,7 @@ public function __construct(EntityManagerInterface $entity_manager, CategoryStor ...@@ -44,7 +51,7 @@ public function __construct(EntityManagerInterface $entity_manager, CategoryStor
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('entity.manager'), $container->get('plugin.manager.entity')->getStorageController('aggregator_feed'),
$container->get('aggregator.category.storage') $container->get('aggregator.category.storage')
); );
} }
...@@ -118,8 +125,7 @@ public function form(array $form, array &$form_state) { ...@@ -118,8 +125,7 @@ public function form(array $form, array &$form_state) {
public function validate(array $form, array &$form_state) { public function validate(array $form, array &$form_state) {
$feed = $this->buildEntity($form, $form_state); $feed = $this->buildEntity($form, $form_state);
// Check for duplicate titles. // Check for duplicate titles.
$feed_storage_controller = $this->entityManager->getStorageController('aggregator_feed'); $result = $this->feedStorageController->getFeedDuplicates($feed);
$result = $feed_storage_controller->getFeedDuplicates($feed);
foreach ($result as $item) { foreach ($result as $item) {
if (strcasecmp($item->title, $feed->label()) == 0) { if (strcasecmp($item->title, $feed->label()) == 0) {
form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label()))); form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\book\Form; namespace Drupal\book\Form;
use Drupal\Core\Entity\ContentEntityFormController; use Drupal\Core\Entity\ContentEntityFormController;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\book\BookManager; use Drupal\book\BookManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -33,25 +32,19 @@ class BookOutlineForm extends ContentEntityFormController { ...@@ -33,25 +32,19 @@ class BookOutlineForm extends ContentEntityFormController {
/** /**
* Constructs a BookOutlineForm object. * Constructs a BookOutlineForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\book\BookManager $book_manager
* The BookManager service.
*/ */
public function __construct(EntityManagerInterface $entity_manager, BookManager $book_manager) { public function __construct(BookManager $bookManager) {
parent::__construct($entity_manager); $this->bookManager = $bookManager;
$this->bookManager = $book_manager;
} }
/** /**
* {@inheritdoc} * This method lets us inject the services this class needs.
*
* Only inject services that are actually needed. Which services
* are needed will vary by the controller.
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static($container->get('book.manager'));
$container->get('entity.manager'),
$container->get('book.manager')
);
} }
/** /**
......
...@@ -896,7 +896,7 @@ function comment_translation_configuration_element_submit($form, &$form_state) { ...@@ -896,7 +896,7 @@ function comment_translation_configuration_element_submit($form, &$form_state) {
$key = 'language_configuration'; $key = 'language_configuration';
$comment_form_state = array( $comment_form_state = array(
'content_translation' => array('key' => $key), 'content_translation' => array('key' => $key),
'language' => array($key => array('entity_type' => 'comment', 'bundle' => $form['#field']->name)), 'language' => array($key => array('entity_type' => 'comment', 'bundle' => $form['#field']['name'])),
'values' => array($key => array('content_translation' => $form_state['values']['content_translation'])), 'values' => array($key => array('content_translation' => $form_state['values']['content_translation'])),
); );
content_translation_language_configuration_element_submit($form, $comment_form_state); content_translation_language_configuration_element_submit($form, $comment_form_state);
......
...@@ -23,6 +23,13 @@ ...@@ -23,6 +23,13 @@
*/ */
class CommentFormController extends ContentEntityFormController { class CommentFormController extends ContentEntityFormController {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/** /**
* The field info service. * The field info service.
* *
...@@ -51,28 +58,13 @@ public static function create(ContainerInterface $container) { ...@@ -51,28 +58,13 @@ public static function create(ContainerInterface $container) {
* @param \Drupal\Core\Session\AccountInterface $current_user * @param \Drupal\Core\Session\AccountInterface $current_user
* The current user. * The current user.
*/ */
public function __construct(EntityManagerInterface $entity_manager, FieldInfo $field_info, AccountInterface $current_user) { public function __construct(EntityManagerInterface $entity_manager, FieldInfo $field_info, AccountInterface $current_user) {
parent::__construct($entity_manager); $this->entityManager = $entity_manager;
$this->fieldInfo = $field_info; $this->fieldInfo = $field_info;
$this->currentUser = $current_user; $this->currentUser = $current_user;
} }
/**
* {@inheritdoc}
*/
protected function init(array &$form_state) {
$comment = $this->entity;
// Make the comment inherit the current content language unless specifically
// set.
if ($comment->isNew()) {
$language_content = \Drupal::languageManager()->getLanguage(Language::TYPE_CONTENT);
$comment->langcode->value = $language_content->id;
}
parent::init($form_state);
}
/** /**
* Overrides Drupal\Core\Entity\EntityFormController::form(). * Overrides Drupal\Core\Entity\EntityFormController::form().
*/ */
...@@ -215,6 +207,13 @@ public function form(array $form, array &$form_state) { ...@@ -215,6 +207,13 @@ public function form(array $form, array &$form_state) {
'#value' => ($comment->id() ? !$comment->uid->target_id : $this->currentUser->isAnonymous()), '#value' => ($comment->id() ? !$comment->uid->target_id : $this->currentUser->isAnonymous()),
); );
// Make the comment inherit the current content language unless specifically
// set.
if ($comment->isNew()) {
$language_content = language(Language::TYPE_CONTENT);
$comment->langcode->value = $language_content->id;
}
// Add internal comment properties. // Add internal comment properties.
$original = $comment->getUntranslated(); $original = $comment->getUntranslated();
foreach (array('cid', 'pid', 'entity_id', 'entity_type', 'field_id', 'uid', 'langcode') as $key) { foreach (array('cid', 'pid', 'entity_id', 'entity_type', 'field_id', 'uid', 'langcode') as $key) {
......
...@@ -23,6 +23,13 @@ ...@@ -23,6 +23,13 @@
*/ */
class CommentViewBuilder extends EntityViewBuilder implements EntityViewBuilderInterface, EntityControllerInterface { class CommentViewBuilder extends EntityViewBuilder implements EntityViewBuilderInterface, EntityControllerInterface {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/** /**
* The field info service. * The field info service.
* *
...@@ -50,7 +57,6 @@ class CommentViewBuilder extends EntityViewBuilder implements EntityViewBuilderI ...@@ -50,7 +57,6 @@ class CommentViewBuilder extends EntityViewBuilder implements EntityViewBuilderI
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static( return new static(
$entity_type, $entity_type,
$entity_info,
$container->get('entity.manager'), $container->get('entity.manager'),
$container->get('field.info'), $container->get('field.info'),
$container->get('module_handler'), $container->get('module_handler'),
...@@ -63,8 +69,6 @@ public static function createInstance(ContainerInterface $container, $entity_typ ...@@ -63,8 +69,6 @@ public static function createInstance(ContainerInterface $container, $entity_typ
* *
* @param string $entity_type * @param string $entity_type
* The entity type. * The entity type.
* @param array $entity_info
* The entity information array.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service. * The entity manager service.
* @param \Drupal\field\FieldInfo $field_info * @param \Drupal\field\FieldInfo $field_info
...@@ -74,8 +78,9 @@ public static function createInstance(ContainerInterface $container, $entity_typ ...@@ -74,8 +78,9 @@ public static function createInstance(ContainerInterface $container, $entity_typ
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
* The CSRF token manager service. * The CSRF token manager service.
*/ */
public function __construct($entity_type, array $entity_info, EntityManagerInterface $entity_manager, FieldInfo $field_info, ModuleHandlerInterface $module_handler, CsrfTokenGenerator $csrf_token) { public function __construct($entity_type, EntityManagerInterface $entity_manager, FieldInfo $field_info, ModuleHandlerInterface $module_handler, CsrfTokenGenerator $csrf_token) {
parent::__construct($entity_type, $entity_info, $entity_manager); parent::__construct($entity_type);
$this->entityManager = $entity_manager;
$this->fieldInfo = $field_info; $this->fieldInfo = $field_info;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->csrfToken = $csrf_token; $this->csrfToken = $csrf_token;
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
use Drupal\comment\CommentManagerInterface; use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityConfirmFormBase; use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -28,13 +27,10 @@ class DeleteForm extends ContentEntityConfirmFormBase { ...@@ -28,13 +27,10 @@ class DeleteForm extends ContentEntityConfirmFormBase {
/** /**
* Constructs a DeleteForm object. * Constructs a DeleteForm object.
* *
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\comment\CommentManagerInterface $comment_manager * @param \Drupal\comment\CommentManagerInterface $comment_manager
* The comment manager service. * The comment manager service.
*/ */
public function __construct(EntityManagerInterface $entity_manager, CommentManagerInterface $comment_manager) { public function __construct(CommentManagerInterface $comment_manager) {
parent::__construct($entity_manager);
$this->commentManager = $comment_manager; $this->commentManager = $comment_manager;
} }
...@@ -43,7 +39,6 @@ public function __construct(EntityManagerInterface $entity_manager, CommentManag ...@@ -43,7 +39,6 @@ public function __construct(EntityManagerInterface $entity_manager, CommentManag
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('entity.manager'),
$container->get('comment.manager') $container->get('comment.manager')
); );
} }
......
...@@ -323,12 +323,7 @@ function content_translation_translate_access(EntityInterface $entity) { ...@@ -323,12 +323,7 @@ function content_translation_translate_access(EntityInterface $entity) {
*/ */
function content_translation_view_access(EntityInterface $entity, $langcode, AccountInterface $account = NULL) { function content_translation_view_access(EntityInterface $entity, $langcode, AccountInterface $account = NULL) {
$entity_type = $entity->entityType(); $entity_type = $entity->entityType();
$info = $entity->entityInfo(); return !empty($entity->translation[$langcode]['status']) || user_access('translate any entity', $account) || user_access("translate $entity_type entities", $account);
$permission = "translate $entity_type";
if (!empty($info['permission_granularity']) && $info['permission_granularity'] == 'bundle') {
$permission = "translate {$entity->bundle()} $entity_type";
}
return !empty($entity->translation[$langcode]['status']) || user_access('translate any entity', $account) || user_access($permission, $account);
} }
/** /**
...@@ -632,10 +627,7 @@ function content_translation_permission() { ...@@ -632,10 +627,7 @@ function content_translation_permission() {
* Implements hook_form_alter(). * Implements hook_form_alter().
*/ */
function content_translation_form_alter(array &$form, array &$form_state) { function content_translation_form_alter(array &$form, array &$form_state) {
$form_controller = content_translation_form_controller($form_state); if (($form_controller = content_translation_form_controller($form_state)) && ($entity = $form_controller->getEntity()) && !$entity->isNew() && $entity instanceof ContentEntityInterface && $entity->isTranslatable()) {
$entity = $form_controller ? $form_controller->getEntity() : NULL;
if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1) {
$controller = content_translation_controller($entity->entityType()); $controller = content_translation_controller($entity->entityType());
$controller->entityFormAlter($form, $form_state, $entity); $controller->entityFormAlter($form, $form_state, $entity);
...@@ -658,17 +650,29 @@ function content_translation_form_alter(array &$form, array &$form_state) { ...@@ -658,17 +650,29 @@ function content_translation_form_alter(array &$form, array &$form_state) {
} }
/** /**
* Implements hook_language_fallback_candidates_OPERATION_alter(). * Implements hook_field_language_alter().
* *
* Performs language fallback for unaccessible translations. * Performs language fallback for unaccessible translations.
*/ */
function content_translation_language_fallback_candidates_entity_view_alter(&$candidates, $context) { function content_translation_field_language_alter(&$display_language, $context) {
$entity = $context['data']; $entity = $context['entity'];
foreach ($entity->getTranslationLanguages() as $langcode => $language) { $entity_type = $entity->entityType();
if (!content_translation_view_access($entity, $langcode)) {
unset($candidates[$langcode]); if ($entity instanceof ContentEntityInterface && isset($entity->translation[$context['langcode']]) && $entity->isTranslatable() && !content_translation_view_access($entity, $context['langcode'])) {
$instances = field_info_instances($entity_type, $entity->bundle());
// Avoid altering the real entity.
$entity = clone($entity);
$entity_langcode = $entity->getUntranslated()->language()->id;
foreach ($entity->translation as $langcode => $translation) {
if ($langcode == $context['langcode'] || !content_translation_view_access($entity, $langcode)) {
$entity->removeTranslation($langcode);
} }
} }
// Find the new fallback values.
field_language_fallback($display_language, $entity, $context['langcode']);
}
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment