Change record status: 
Project: 
Introduced in branch: 
11.x
Description: 

For Views to maintain compatibility with Postgresql we have introduced a short hash of original alias of the name (within the first 60 characters), and truncate the result to 60 characters.

Example usage

   $alias = $table . '_' . $field;
   $alias = $this->sanitizeAlias($alias);

New static function

  /**
   * Produce a safe alias value.
   *
   * We limit the length of an alias string (to a default maximum of 60
   * characters), incorporating a hash of the original value for uniqueness
   * if that value was too long to use verbatim.
   *
   * This prevents subsequent truncation from creating duplicate aliases
   * in cases where two or more aliases are identical up to the point of
   * truncation.  This happens particularly easily with relationships,
   * where alias names may be built from several concatenated identifiers.
   *
   * @param mixed $alias
   *   The alias to sanitize.
   * @param int $maxlength
   *   The maximum number of characters permitted in an alias. Defaults to 60.
   *
   * @return mixed
   *   The sanitized alias string (if a string argument was supplied;
   *   non-strings are returned unmodified).
   */
  public static function sanitizeAlias(mixed $alias, int $maxlength = 60): mixed {
    if (!is_string($alias)) {
      return $alias;
    }
    if (strlen($alias) > $maxlength) {
      $checksum = 'c' . crc32($alias);
      $pos = strlen($checksum) + strlen($alias) - $maxlength;
      $alias = $checksum . substr($alias, $pos);
    }
    return strtolower(substr($alias, 0, $maxlength));
  }
Impacts: 
Site builders, administrators, editors
Module developers