reviewer-1.2.x-dev/modules/reviewer_test_kit/src/Plugin/reviewer/Task/Entity/Display/FieldSettingsTaskBase.php
modules/reviewer_test_kit/src/Plugin/reviewer/Task/Entity/Display/FieldSettingsTaskBase.php
<?php
declare(strict_types=1);
namespace Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display;
use Drupal\Component\Utility\NestedArray;
use Drupal\reviewer\Reviewer\Result\ResultInterface;
use Drupal\reviewer\Reviewer\Task\FixableInterface;
/**
* Internal task for checking the field widget settings on entity display forms.
*
* @internal
* This class is internal and the methods within may change at any time. Use
* \Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\Form\FieldWidgetSettingsTaskBase
* or
* \Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\View\FieldFormatterSettingsTaskBase
* to check if fields are disabled on specific entity displays instead.
*
* @see \Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\Form\FieldWidgetSettingsTaskBase
* @see \Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display\View\FieldFormatterSettingsTaskBase
*/
abstract class FieldSettingsTaskBase extends DisplayFieldsTaskBase implements FixableInterface {
/**
* The expected settings in the same structure as the field settings.
*
* Only listed values are checked, omitted values are ignored. String values
* can be negated by prefixing them with '!'. Negated values match anything
* but the string. Negated values are also unfixable because there is no
* exact value to set.
*
* See the content.* keys in the core.entity_(form|view)_display YAMLs for the
* structure and values of the field type's settings array.
*
* @var array<string, array|scalar>
*/
protected array $expectedValues = [];
/**
* {@inheritdoc}
*/
protected function checkValidProperties(): void {
parent::checkValidProperties();
if (\count($this->expectedValues) === 0) {
throw new \LogicException(sprintf('Property %s::expectedValues cannot be empty.', static::class));
}
}
/**
* {@inheritdoc}
*/
public function check(): ResultInterface {
$this->checkValidProperties();
$results = $this->createCollection();
foreach ($this->displays() as $display) {
$components = $this->fieldsCallback($display);
foreach ($this->fieldsToCheck($display) as $field) {
$settings = $components[$field];
$errors = [];
$parents = [];
foreach ($this->expectedValues($this->expectedValues, $parents) as $expected) {
$passed = NestedArray::getValue($settings, $parents) === $this->stripNegation($expected);
if ($this->negated($expected)) {
$passed = !$passed;
}
if (!$passed) {
$errors[] = implode('.', [$this->entityTypeFullId(), $display->getMode(), $field, implode('.', $parents)]);
}
}
$results->add($this->createCheckResult(
\count($errors) === 0,
sprintf('Settings for field "%s" are correct.', $field),
sprintf('Settings for field "%s" are incorrect.', implode('", "', $errors)),
"{$display->getMode()}.$field",
));
}
}
return $results;
}
/**
* {@inheritdoc}
*/
public function fix(): ResultInterface {
$negated = [];
foreach ($this->displays() as $display) {
$components = $this->fieldsCallback($display);
foreach ($this->fieldsToCheck($display) as $field) {
$parents = [];
$settings = $components[$field];
foreach ($this->expectedValues($this->expectedValues, $parents) as $expected) {
// Negated values cannot be corrected, because we can only tell what a
// correct value is not, what a correct value is.
if ($this->negated($expected)) {
$negated[] = implode('.', [$this->entityTypeFullId(), $display->getMode(), $field, implode('.', $parents)]);
continue;
}
NestedArray::setValue($settings, $parents, $expected);
}
$this->fixSaveCallback($display, $field, $settings);
}
$display->save();
}
$failure_text = \count($negated) === 0
? sprintf('Unable to fix field all settings for fields in "%s".', $this->entityTypeFullId())
: sprintf('Unable to fix field setting "%s" due to negated values.', implode('", "', $negated));
return $this->createFixResult(
$this->check()->getStatus(),
sprintf('Fixed field settings for "%s".', $this->entityTypeFullId()),
$failure_text,
);
}
}
