Commit 0d27b063 authored by alexpott's avatar alexpott

Issue #2845151 by Sam152, timmillwood, tstoeckler:...

Issue #2845151 by Sam152, timmillwood, tstoeckler: ContentModerationStateFormatter pretends it's for the moderated entity, but it is for the content moderation state entity
parent eb83216e
......@@ -2,9 +2,12 @@
namespace Drupal\content_moderation\Plugin\Field\FieldFormatter;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'content_moderation_state' formatter.
......@@ -17,24 +20,50 @@
* }
* )
*/
class ContentModerationStateFormatter extends FormatterBase {
class ContentModerationStateFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The moderation information service.
*
* @var \Drupal\content_moderation\ModerationInformationInterface
*/
protected $moderationInformation;
/**
* Create an instance of ContentModerationStateFormatter.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModerationInformationInterface $moderation_information) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->moderationInformation = $moderation_information;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('content_moderation.moderation_information')
);
}
$entity = $items->getEntity();
$workflow = $entity->workflow->entity;
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$workflow = $this->moderationInformation->getWorkflowForEntity($items->getEntity());
foreach ($items as $delta => $item) {
if (!$item->isEmpty()) {
$elements[$delta] = [
'#markup' => $workflow->getState($item->value)->label(),
];
}
$elements[$delta] = [
'#markup' => $workflow->getState($item->value)->label(),
];
}
return $elements;
}
......@@ -42,7 +71,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition->getName() === 'moderation_state';
return $field_definition->getName() === 'moderation_state' && $field_definition->getTargetEntityTypeId() !== 'content_moderation_state';
}
}
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\Core\Render\RenderContext;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\KernelTests\KernelTestBase;
use Drupal\workflows\Entity\Workflow;
/**
* Test the state field formatter.
*
* @group content_moderation
*/
class StateFormatterTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'workflows',
'content_moderation',
'entity_test',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('entity_test_rev');
$this->installEntitySchema('content_moderation_state');
$this->installConfig('content_moderation');
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
$workflow->save();
}
/**
* Test the embed field.
*
* @dataProvider formatterTestCases
*/
public function testStateFieldFormatter($field_value, $formatter_settings, $expected_output) {
$entity = EntityTestRev::create([
'moderation_state' => $field_value,
]);
$entity->save();
$field_output = $this->container->get('renderer')->executeInRenderContext(new RenderContext(), function() use ($entity, $formatter_settings) {
return $entity->moderation_state->view($formatter_settings);
});
$this->assertEquals($expected_output, $field_output[0]);
}
/**
* Test cases for ::
*/
public function formatterTestCases() {
return [
'Draft State' => [
'draft',
[
'type' => 'content_moderation_state',
'settings' => [],
],
[
'#markup' => 'Draft',
],
],
'Published State' => [
'published',
[
'type' => 'content_moderation_state',
'settings' => [],
],
[
'#markup' => 'Published',
],
],
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment