Skip to content
Snippets Groups Projects

hide comment field after entity build

Open Sudhir Krishna requested to merge issue/drupal-3145146:3145146-on-the-translated into 11.x
2 unresolved threads

Closes #3145146

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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;
  • Wonder if there's a better way to go about this, don't see any instance in core of using this apporach "$build['#' . "

  • 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
  • Please register or sign in to reply
  • quietone
  • 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.
    • Comment on lines +540 to +542

      I am not sure what this means. Is this saying that the module content_translation is installed? Does 'translated content available' mean that the entity has translation enabled?

    • If the entity has been translated then the translated version is loaded and original entity is discarded. Therefore setting the comment field status to hidden is lost. The hidden status must be set after the translated entity is loaded.

    • Re-looking at this does the comment belong here? There is no check actually happening. But if we keep what about shortening to just

      "When translated content is available the entity is replaced with the translated entity. Set the status to hidden during preview."

    • A check is not required. The status must be set on the entity instance that gets used. The comment was provided as clarification on why the pervious code didn't work.

    • So we have git blame for that. Believe the comment should say what the code is doing vs explaining what it's fixing.

    • 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

    • Please register or sign in to reply
  • added 1 commit

    • 2b5387e9 - setup tasks based on api and suggestion applied

    Compare with previous version

  • Sudhir Krishna added 203 commits

    added 203 commits

    Compare with previous version

  • IMMACULATE X added 1 commit

    added 1 commit

    • 470007fc - Update file EntityViewBuilder.php

    Compare with previous version

  • Please register or sign in to reply
    Loading