Commit f12e9aa7 authored by Chris Burge's avatar Chris Burge
Browse files

Issue #3313270 by Chris Burge, jcontreras, mherchel: [PHP 8.1] Deprecated...

Issue #3313270 by Chris Burge, jcontreras, mherchel: [PHP 8.1] Deprecated function: Calling static trait method Drupal\field_css\Traits\CssTrait::itemPrefixes
parent 4d1716d6
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -17,7 +17,12 @@ use Drupal\field_css\Traits\CssTrait;
 * Implements hook_entity_view_alter().
 */
function field_css_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  $prefixes = CssTrait::itemPrefixes($entity, $display->getMode());
  $prefixes = (
    new class() {
      use CssTrait;

    }
  )->itemPrefixes($entity, $display->getMode());

  if ($prefixes) {
    foreach ($prefixes as $prefix) {
+6 −6
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ trait CssTrait {
   * @return array
   *   The prefixes to be added to the item.
   */
  public static function itemPrefixes(ContentEntityInterface $entity, string $view_mode) {
  public function itemPrefixes(ContentEntityInterface $entity, string $view_mode) {
    $return = [];

    $fields = $entity->getFieldDefinitions();
@@ -36,7 +36,7 @@ trait CssTrait {
            ->get('content')[$field->getName()]['settings'];

          if ($formatter_settings['prefix'] == 'entity-item') {
            $return[] = CssTrait::generatePrefix($entity);
            $return[] = self::generatePrefix($entity);
          }
          elseif ($formatter_settings['prefix'] == 'fixed-value') {
            $return[] = $formatter_settings['fixed_prefix_value'];
@@ -60,7 +60,7 @@ trait CssTrait {
   * @return string
   *   A scoped prefix.
   */
  public static function generatePrefix(ContentEntityInterface $entity, $leading_period = FALSE) {
  public function generatePrefix(ContentEntityInterface $entity, $leading_period = FALSE) {
    $prefix = Html::cleanCssIdentifier('scoped-css--' . $entity->getEntityTypeId() . '-' . $entity->id());
    return ($leading_period) ? '.' . $prefix : $prefix;
  }
@@ -76,7 +76,7 @@ trait CssTrait {
   * @return string
   *   The CSS code block with selectors prefixed.
   */
  public static function addSelectorPrefix($css_code, $prefix) {
  public function addSelectorPrefix($css_code, $prefix) {
    $parser = new Parser($css_code);
    $css_document = $parser->parse();
    foreach ($css_document->getAllDeclarationBlocks() as $block) {
@@ -93,7 +93,7 @@ trait CssTrait {
   * Formats CSS.
   *
   * This method should be used in instances where CSS code is not run
   * through CssTrait::addSelectorPrefix().
   * through CssTrait->addSelectorPrefix().
   *
   * @param string $css_code
   *   The CSS code block to be processed.
@@ -101,7 +101,7 @@ trait CssTrait {
   * @return string
   *   The formatted CSS code.
   */
  public static function formatCss($css_code) {
  public function formatCss($css_code) {
    $parser = new Parser($css_code);
    $css_document = $parser->parse();
    return $css_document->render(OutputFormat::createPretty());
+7 −5
Original line number Diff line number Diff line
@@ -12,8 +12,10 @@ use Drupal\field_css\Traits\CssTrait;
 */
class CssTraitTest extends UnitTestCase {

  use CssTrait;

  /**
   * Tests CssTrait::addSelectorPrefix().
   * Tests CssTrait->addSelectorPrefix().
   *
   * Both the selector prefixing and the CSS formatting returned from
   * OutputFormat::createPretty() are verified.
@@ -22,7 +24,7 @@ class CssTraitTest extends UnitTestCase {
    $prefix = '.test-prefix';
    $css_code = 'p { color: green; }' . PHP_EOL . '#id-selector .class-selector { margin: 1em; }';
    $expected_return = PHP_EOL . '.test-prefix p {' . PHP_EOL . '	color: green;' . PHP_EOL . '}' . PHP_EOL . PHP_EOL . '.test-prefix #id-selector .class-selector {' . PHP_EOL . '	margin: 1em;' . PHP_EOL . '}' . PHP_EOL;
    $actual_return = CssTrait::addSelectorPrefix($css_code, $prefix);
    $actual_return = $this->addSelectorPrefix($css_code, $prefix);
    $this->assertSame($expected_return, $actual_return);

    // Verify CSS code is processed by OutputFormat::createPretty().
@@ -30,12 +32,12 @@ class CssTraitTest extends UnitTestCase {
    // by OutputFormat::createPretty().
    $css_code = 'p { color:  green; }' . PHP_EOL . '#id-selector .class-selector { margin: 1em; }';
    $expected_return = PHP_EOL . '.test-prefix p {' . PHP_EOL . '	color: green;' . PHP_EOL . '}' . PHP_EOL . PHP_EOL . '.test-prefix #id-selector .class-selector {' . PHP_EOL . '	margin: 1em;' . PHP_EOL . '}' . PHP_EOL;
    $actual_return = CssTrait::addSelectorPrefix($css_code, $prefix);
    $actual_return = $this->addSelectorPrefix($css_code, $prefix);
    $this->assertSame($expected_return, $actual_return);
  }

  /**
   * Tests CssTrait::formatCss().
   * Tests CssTrait->formatCss().
   *
   * Verify CSS code is processed by OutputFormat::createPretty().
   */
@@ -44,7 +46,7 @@ class CssTraitTest extends UnitTestCase {
    // by OutputFormat::createPretty().
    $css_code = 'p { color:  green; }' . PHP_EOL . '#id-selector .class-selector { margin: 1em; }';
    $expected_return = PHP_EOL . 'p {' . PHP_EOL . '	color: green;' . PHP_EOL . '}' . PHP_EOL . PHP_EOL . '#id-selector .class-selector {' . PHP_EOL . '	margin: 1em;' . PHP_EOL . '}' . PHP_EOL;
    $actual_return = CssTrait::formatCss($css_code);
    $actual_return = $this->formatCss($css_code);
    $this->assertSame($expected_return, $actual_return);
  }