hide comment field after entity build
Closes #3145146
Merge request reports
Activity
533 533 // the commented entity is rendered, it excludes the comment field output. 534 534 $field_name = $comment->getFieldName(); 535 535 $entity = clone $entity; 536 $entity->$field_name->status = CommentItemInterface::HIDDEN; 537 536 $build = \Drupal::entityTypeManager() 538 537 ->getViewBuilder($entity->getEntityTypeId()) 539 538 ->view($entity, 'full'); 539 540 // If content translation is enabled and translated content 541 // is available then the entity is replaced with the translated entity. 542 // Set the status to hidden after the build is done. 543 $build['#' . $entity->getEntityTypeId()]->$field_name->status = CommentItemInterface::HIDDEN; There is no other way to return the translated node that was loaded and attached to the build (without changing multiple functions). Also it's not possible to find the comment field name (without changing multiple functions) in the function where entity is loaded to set the status.
Edited by Sudhir Krishna
- Resolved by Stephen Mustgrave
533 533 // the commented entity is rendered, it excludes the comment field output. 534 534 $field_name = $comment->getFieldName(); 535 535 $entity = clone $entity; 536 $entity->$field_name->status = CommentItemInterface::HIDDEN; 537 536 $build = \Drupal::entityTypeManager() 538 537 ->getViewBuilder($entity->getEntityTypeId()) 539 538 ->view($entity, 'full'); 539 540 // If content translation is enabled and translated content 541 // is available then the entity is replaced with the translated entity. 542 // Set the status to hidden after the build is done. What you say is true for straight forward cases. In cases like this where the reason for the fix or line of code is not straight forward, it's important to explain the behaviour and consequently the fix. Case in point, see same file lines 525 to 533. It explains the behaviour and consequently the fix needed.
This change looks to be reversing the original problem (that the comment above it speaks to).
The issue we have with setting the value on the cloned entity is that we have this code in EntityViewBuilder::viewMultiple
/** * {@inheritdoc} */ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) { $build_list = [ '#sorted' => TRUE, '#pre_render' => [[$this, 'buildMultiple']], ]; $weight = 0; foreach ($entities as $key => $entity) { // Ensure that from now on we are dealing with the proper translation // object. $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); // Set build defaults. $build_list[$key] = $this->getBuildDefaults($entity, $view_mode); $entityType = $this->entityTypeId; $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode); $build_list[$key]['#weight'] = $weight++; } return $build_list;
Specifically the call to
::getTranslationFromContext
which will get the translated version.I think the fix instead should be something like this:
$field_name = $comment->getFieldName(); $entity = clone $entity; foreach ($entity->getTranslationLanguages() as $language) { $translation = $entity->getTranslation($language->getId()); $translation->$field_name->status = CommentItemInterface::HIDDEN; } $build = \Drupal::entityTypeManager() ->getViewBuilder($entity->getEntityTypeId()) ->view($entity, 'full');
i.e. we make the same change on each translation as we do on the default in HEAD
added 1 commit
- 2b5387e9 - setup tasks based on api and suggestion applied
added 203 commits
-
2b5387e9...81ff548f - 202 commits from branch
project:11.x
- 2bfdabde - Merge branch drupal:11.x into 3145146-on-the-translated
-
2b5387e9...81ff548f - 202 commits from branch