Chris@0: installEntitySchema('entity_test'); Chris@0: $this->installEntitySchema('user'); Chris@0: $this->installEntitySchema('comment'); Chris@0: $this->installSchema('dblog', ['watchdog']); Chris@0: Chris@0: // Create a new 'comment' comment-type. Chris@0: CommentType::create([ Chris@0: 'id' => 'comment', Chris@0: 'label' => $this->randomString(), Chris@0: ])->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests view mode setting integration. Chris@0: * Chris@0: * @see comment_entity_view_display_presave() Chris@0: * @see CommentDefaultFormatter::calculateDependencies() Chris@0: */ Chris@0: public function testViewMode() { Chris@17: $mode = mb_strtolower($this->randomMachineName()); Chris@0: // Create a new comment view mode and a view display entity. Chris@0: EntityViewMode::create([ Chris@0: 'id' => "comment.$mode", Chris@0: 'targetEntityType' => 'comment', Chris@0: 'settings' => ['comment_type' => 'comment'], Chris@0: ])->save(); Chris@0: EntityViewDisplay::create([ Chris@0: 'targetEntityType' => 'comment', Chris@0: 'bundle' => 'comment', Chris@0: 'mode' => $mode, Chris@0: ])->setStatus(TRUE)->save(); Chris@0: Chris@0: // Create a comment field attached to a host 'entity_test' entity. Chris@0: FieldStorageConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'comment', Chris@17: 'field_name' => $field_name = mb_strtolower($this->randomMachineName()), Chris@0: 'settings' => [ Chris@0: 'comment_type' => 'comment', Chris@0: ], Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: 'field_name' => $field_name, Chris@0: ])->save(); Chris@0: Chris@0: $component = [ Chris@0: 'type' => 'comment_default', Chris@0: 'settings' => ['view_mode' => $mode, 'pager_id' => 0], Chris@0: ]; Chris@0: // Create a new 'entity_test' view display on host entity that uses the Chris@0: // custom comment display in field formatter to show the field. Chris@0: EntityViewDisplay::create([ Chris@0: 'targetEntityType' => 'entity_test', Chris@0: 'bundle' => 'entity_test', Chris@0: 'mode' => 'default', Chris@0: ])->setComponent($field_name, $component)->setStatus(TRUE)->save(); Chris@0: Chris@0: $host_display_id = 'entity_test.entity_test.default'; Chris@0: $comment_display_id = "comment.comment.$mode"; Chris@0: Chris@0: // Disable the "comment.comment.$mode" display. Chris@0: EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save(); Chris@0: Chris@0: /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */ Chris@0: $host_display = EntityViewDisplay::load($host_display_id); Chris@0: Chris@0: // Check that the field formatter has been disabled on host view display. Chris@0: $this->assertNull($host_display->getComponent($field_name)); Chris@0: $this->assertTrue($host_display->get('hidden')[$field_name]); Chris@0: Chris@0: // Check that the proper warning has been logged. Chris@0: $arguments = [ Chris@0: '@id' => $host_display_id, Chris@0: '@name' => $field_name, Chris@0: '@display' => EntityViewMode::load("comment.$mode")->label(), Chris@0: '@mode' => $mode, Chris@0: ]; Chris@0: $logged = (bool) Database::getConnection()->select('watchdog') Chris@0: ->fields('watchdog', ['wid']) Chris@0: ->condition('type', 'system') Chris@0: ->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.") Chris@0: ->condition('variables', serialize($arguments)) Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: $this->assertTrue($logged); Chris@0: Chris@0: // Re-enable the comment view display. Chris@0: EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save(); Chris@0: // Re-enable the comment field formatter on host entity view display. Chris@0: EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save(); Chris@0: Chris@0: // Delete the "comment.$mode" view mode. Chris@0: EntityViewMode::load("comment.$mode")->delete(); Chris@0: Chris@0: // Check that the comment view display entity has been deleted too. Chris@0: $this->assertNull(EntityViewDisplay::load($comment_display_id)); Chris@0: Chris@0: /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ Chris@0: $host_display = EntityViewDisplay::load($host_display_id); Chris@0: Chris@0: // Check that the field formatter has been disabled on host view display. Chris@0: $this->assertNull($host_display->getComponent($field_name)); Chris@0: $this->assertTrue($host_display->get('hidden')[$field_name]); Chris@0: } Chris@0: Chris@0: }