Problem/Motivation

If you add a custom field to an entity type supported by views and the field starts with a number then \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace() will fail it's assertions if you rewrite the output of a field. This because the field starts with a number and fails this regex:

assert(preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $token) === 1, 'Tokens need to be valid Twig variables.');

Steps to reproduce

Note this can only occur on base fields as configurable fields will error - see \Drupal\field\Entity\FieldStorageConfig::__construct()

See test added by MR.

Proposed resolution

Either fix views... or deprecate base fields with such names... but renaming base fields for existing sites is not that easy.

Current solution fixes views.

Remaining tasks

User interface changes

None

Introduced terminology

N/a

API changes

None

Data model changes

None

Release notes snippet

N/a

Issue fork drupal-3548695

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

alexpott’s picture

In \Drupal\field\Entity\FieldStorageConfig::__construct() we do:

    if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $values['field_name'])) {
      throw new FieldException("Attempt to create a field storage {$values['field_name']} with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character");
    }

I wonder if we should implement something similar in \Drupal\Core\Field\BaseFieldDefinition::setName() - or consider deprecating support for such field names - so we can remove the foot gun.

alexpott’s picture

Issue summary: View changes
Status: Active » Needs review
alexpott’s picture

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Appears to have some test failures

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.