Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drupal 10 compatibility #3

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
on: pull_request
name: PR Review
jobs:
changelog:
runs-on: ubuntu-latest
name: Changelog should be updated
strategy:
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Git fetch
run: git fetch

- name: Check that changelog has been updated.
run: git diff --exit-code origin/${{ github.base_ref }} -- CHANGELOG.md && exit 1 || exit 0

test-composer-files:
name: Validate composer
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Validate composer files
run: |
composer validate --strict composer.json
# Check that dependencies resolve.
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

php-check-coding-standards:
name: PHP - Check Coding Standards
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: |
composer install --no-interaction --no-progress
- name: PHPCS
run: |
composer coding-standards-check/phpcs

php-code-analysis:
name: PHP code analysis
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json, gd
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: drupal-check
run: |
# We need a Drupal project to run drupal-check (cf. https://github.com/mglaman/drupal-check#usage)
# Install Drupal
composer --no-interaction create-project drupal/recommended-project:^10 drupal
# Copy our module source code into the Drupal module folder.
mkdir -p drupal/web/modules/contrib/os2forms_user_field_lookup
cp -r os2forms_user_field_lookup.* composer.json src drupal/web/modules/contrib/os2forms_user_field_lookup

composer --working-dir=drupal --no-interaction config minimum-stability dev

# Allow all plugins
composer --working-dir=drupal config --no-plugins allow-plugins true

# Add our module as a composer repository.
composer --working-dir=drupal --no-interaction config --append repositories.itk-dev/os2forms_user_field_lookup path web/modules/contrib/os2forms_user_field_lookup
# Restore Drupal composer repository.
# composer --working-dir=drupal --no-interaction config --append repositories.drupal composer https://packages.drupal.org/8

# Make Drupal 10 compatible
composer --working-dir=drupal --no-interaction require psr/http-message:^1.0
composer --working-dir=drupal --no-interaction require 'mglaman/composer-drupal-lenient'
composer --working-dir=drupal config --no-plugins --merge --json extra.drupal-lenient.allowed-list '["drupal/coc_forms_auto_export", "drupal/webform_node_element"]'

# Require our module.
composer --working-dir=drupal --no-interaction require 'itk-dev/os2forms_user_field_lookup:*'

# Check code
composer --working-dir=drupal --no-interaction require --dev drupal/core-dev
cd drupal/web/modules/contrib/os2forms_user_field_lookup
# Remove our non-dev dependencies to prevent duplicated Drupal installation
# PHP Fatal error: Cannot redeclare drupal_get_filename() (previously declared in /home/runner/work/os2forms_user_field_lookup/os2forms_user_field_lookup/drupal/web/modules/contrib/os2forms_user_field_lookup/vendor/drupal/core/includes/bootstrap.inc:190) in /home/runner/work/os2forms_user_field_lookup/os2forms_user_field_lookup/drupal/web/core/includes/bootstrap.inc on line 190
# Use sed to remove the "require" property in composer.json
sed -i '/^\s*"require":/,/^\s*}/d' composer.json
composer --no-interaction install
composer code-analysis
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- markdownlint-disable MD024 -->
# Changelog

All notable changes to this project will be documented in this file.

See [keep a changelog](https://keepachangelog.com/en/1.0.0/) for information
about writing changes to this log.

## [Unreleased]

## [1.1.0] 2024-09-12

- Drupal 10 compatibility.
- Added GitHub Actions.

## [1.0.0]

- Initial version

[Unreleased]: https://github.com/itk-dev/os2forms_user_field_lookup/compare/1.1.0...HEAD
[1.1.0]: https://github.com/itk-dev/os2forms_user_field_lookup/compare/1.0.0...1.1.0
[1.0.0]: https://github.com/itk-dev/os2forms_user_field_lookup/releases/tag/1.0.0
54 changes: 51 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,60 @@
],
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
}
],
"require": {
"os2forms/os2forms": "^3.13"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
"drupal/coder": "^8.3",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1"
"mglaman/drupal-check": "^1.4"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"zaporylie/composer-drupal-optimizations": true,
"cweagans/composer-patches": true,
"simplesamlphp/composer-module-installer": true
}
},
"extra": {
"patches": {
"drupal/webform_node_element": {
"3290637 - Automated Drupal 10 compatibility fixes": "https://www.drupal.org/files/issues/2023-05-12/webform_node_element_d10-3290637-11.patch"
}
},
"drupal-lenient": {
"allowed-list": [
"drupal/coc_forms_auto_export",
"drupal/webform_node_element"
]
}
},
"scripts": {
"coding-standards-check": "phpcs --standard=phpcs.xml.dist",
"coding-standards-apply": "phpcbf --standard=phpcs.xml.dist"
"code-analysis/drupal-check": [
"vendor/bin/drupal-check --deprecations --analysis --exclude-dir=vendor *.* src"
],
"code-analysis": [
"@code-analysis/drupal-check"
],
"coding-standards-check/phpcs": [
"vendor/bin/phpcs --standard=phpcs.xml.dist"
],
"coding-standards-check": [
"@coding-standards-check/phpcs"
],
"coding-standards-apply/phpcs": [
"vendor/bin/phpcbf --standard=phpcs.xml.dist"
],
"coding-standards-apply": [
"@coding-standards-apply/phpcs"
]
}
}
6 changes: 4 additions & 2 deletions os2forms_user_field_lookup.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ name: OS2Forms User Field Lookup
type: module
description: Use user field data in webforms.
package: OS2Forms
core: 8.x
core_version_requirement: ^8 || ^9
core_version_requirement: ^9 || ^10
dependencies:
- drupal:webform
- os2forms:os2forms
14 changes: 11 additions & 3 deletions os2forms_user_field_lookup.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@ use Drupal\os2forms_user_field_lookup\WebformElementHelper;
* Implements hook_webform_element_default_properties_alter().
*
* @see WebformElementHelper::alterDefaultProperties()
*
* @phpstan-param array<string, mixed> $properties
* @phpstan-param array<string, mixed> $definition
*/
function os2forms_user_field_lookup_webform_element_default_properties_alter(array &$properties, array &$definition) {
function os2forms_user_field_lookup_webform_element_default_properties_alter(array &$properties, array &$definition): void {
Drupal::service(WebformElementHelper::class)->alterDefaultProperties($properties, $definition);
}

/**
* Implements hook_webform_element_translatable_properties_alter().
*
* @see WebformElementHelper::alterTranslatableProperties()
*
* @phpstan-param array<string, mixed> $properties
* @phpstan-param array<string, mixed> $definition
*/
function os2forms_user_field_lookup_webform_element_translatable_properties_alter(array &$properties, array &$definition) {
function os2forms_user_field_lookup_webform_element_translatable_properties_alter(array &$properties, array &$definition): void {
Drupal::service(WebformElementHelper::class)->alterTranslatableProperties($properties, $definition);
}

/**
* Implements hook_webform_element_configuration_form_alter().
*
* @see WebformElementHelper::alterConfigurationForm()
*
* @phpstan-param array<string, mixed> $form
*/
function os2forms_user_field_lookup_webform_element_configuration_form_alter(&$form, FormStateInterface $form_state) {
function os2forms_user_field_lookup_webform_element_configuration_form_alter(&$form, FormStateInterface $form_state): void {
Drupal::service(WebformElementHelper::class)->alterConfigurationForm($form, $form_state);
}
5 changes: 4 additions & 1 deletion src/Element/UserFieldElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ class UserFieldElement extends Textfield {

/**
* {@inheritDoc}
*
* @phpstan-param array<string, mixed> $element
* @phpstan-return array<string, mixed>
*/
public static function preRenderTextfield($element) {
public static function preRenderTextfield($element): array {
$element = parent::preRenderTextfield($element);
static::setAttributes($element, ['os2forms-user-field-lookup']);

Expand Down
5 changes: 4 additions & 1 deletion src/Element/UserFieldElementCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ class UserFieldElementCheckbox extends Checkbox {

/**
* {@inheritDoc}
*
* @phpstan-param array<string, mixed> $element
* @phpstan-return array<string, mixed>
*/
public static function preRenderCheckbox($element) {
public static function preRenderCheckbox($element): array {
$element = parent::preRenderCheckbox($element);
// @see https://stackoverflow.com/a/6905050
$element['#attributes']['onclick'] = 'return false';
Expand Down
9 changes: 7 additions & 2 deletions src/Plugin/WebformElement/UserFieldElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Drupal\os2forms_user_field_lookup\Plugin\WebformElement;

use Drupal\Component\Utility\NestedArray;
use Drupal\webform\Plugin\WebformElement\TextField;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\TextField;

/**
* User field element.
Expand All @@ -20,6 +20,8 @@ class UserFieldElement extends TextField {

/**
* {@inheritdoc}
*
* @phpstan-return array<string, mixed>
*/
protected function defineDefaultProperties() {
return [
Expand All @@ -29,8 +31,11 @@ protected function defineDefaultProperties() {

/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $element
* @phpstan-param array<string, mixed> $form
*/
public function alterForm(array &$element, array &$form, FormStateInterface $form_state) {
public function alterForm(array &$element, array &$form, FormStateInterface $form_state): void {
if ($fieldName = $element['#os2forms_user_field_lookup_field_name'] ?? NULL) {
if ($this->currentUser->isAuthenticated()) {
/** @var \Drupal\user\Entity\User $user */
Expand Down
7 changes: 6 additions & 1 deletion src/Plugin/WebformElement/UserFieldElementCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class UserFieldElementCheckbox extends Checkbox {

/**
* {@inheritdoc}
*
* @phpstan-return array<string, mixed>
*/
protected function defineDefaultProperties() {
return [
Expand All @@ -29,8 +31,11 @@ protected function defineDefaultProperties() {

/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $element
* @phpstan-param array<string, mixed> $form
*/
public function alterForm(array &$element, array &$form, FormStateInterface $form_state) {
public function alterForm(array &$element, array &$form, FormStateInterface $form_state): void {
if ($fieldName = $element['#os2forms_user_field_lookup_field_name'] ?? NULL) {
if ($this->currentUser->isAuthenticated()) {
/** @var \Drupal\user\Entity\User $user */
Expand Down
Loading
Loading