Patch für Modul installieren?
Eingetragen von Tropse (111)
am 13.02.2022 - 01:22 Uhr in
am 13.02.2022 - 01:22 Uhr in
Hallo,
wie bekommt man am unkompliziertesten einen Patch in ein Modul?
Es geht um diesen Patch: https://www.drupal.org/project/bulk_user_registration/issues/3259418
diff --git a/src/Form/BulkUserImport.php b/src/Form/BulkUserImport.php
index 99c4c90..09116e5 100644
--- a/src/Form/BulkUserImport.php
+++ b/src/Form/BulkUserImport.php
@@ -190,8 +190,8 @@ class BulkUserImport extends FormBase {
$operations[] = [
'\Drupal\bulk_user_registration\Form\BulkUserImport::batchImport',
[
- 'userData' => $data,
- 'defaultRole' => $defaultRole,
+ $data,
+ $defaultRole,
],
];
}
Das Patch Modul ist installiert:
composer require cweagans/composer-patches
Wie hier beschrieben: https://github.com/cweagans/composer-patches
Habe ich versucht das in die composer.json einzubauen:
"extra": {
"merge-plugin": {
"include": [
"web/modules/contrib/webform/composer.libraries.json"
]
},
"patches": {
"drupal/core": {
"Add startup configuration for PHP server": "https://www.drupal.org/files/issues/2022-01-19/batch-error-with-php-version-8-3259418-2.patch"
}
},
"drupal-scaffold": {
"locations": {
"web-root": "web/"
}
},
Es kommt diese Fehlermeldung:
Gathering patches for dependencies. This might take a minute.
- Installing drupal/core (9.3.5): Extracting archive
- Applying patches for drupal/core
https://www.drupal.org/files/issues/2022-01-19/batch-error-with-php-vers... (Add startup configuration for PHP server)
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2022-01-19/batch-error-with-php-version-8-3259418-2.patch
Danke.
- Anmelden oder Registrieren um Kommentare zu schreiben
wenn es nur so wenige
am 15.02.2022 - 07:51 Uhr
wenn es nur so wenige Änderungen sind, mache ich das per Hand.
Also in Deinem Fall:
Datei suchen, die beiden Zeilen mit Minus entfernen, die beiden Zeilen mit Plus vorne das Plus entfernen.
Evt. einen Kommentar dazu und irgendwo in den Unterlagen dokumentieren.
Größere Patches habe ich auch schon auf der console eingespielt:
https://wiki.ubuntuusers.de/patch/
LG Regina Oswald
-------------------------
Montviso - Internetdienstleistungen
http://www.montviso.de
Schade
am 16.02.2022 - 22:23 Uhr
Danke für den Tipp!
Auch nach zwei Stolpersteinen funktioniert der Patch bei mir nicht.
$operations[] = [
'\Drupal\bulk_user_registration\Form\BulkUserImport::batchImport',
[
$data,
$defaultRole,
],
];
}
Schade.
Drupal 9.x, Ubuntu
In deiner composer.json
am 17.02.2022 - 18:53 Uhr
In deiner composer.json versuchst du anscheinend den Patch in den Core einzubauen, er gehört aber in die Datei
BulkUserImport.php
im Modul Bulk User Registration.So sagt es auch die Fehlermeldung:
- Applying patches for drupal/core
: Der patch wird versucht im Core unterzubringen, da passt er aber nicht dazu.So sollte das in der composer.json aussehen:
"extra": {
"merge-plugin": {
"include": [
"web/modules/contrib/webform/composer.libraries.json"
]
},
"patches": {
"drupal/bulk_user_registration": {
"Add startup configuration for PHP server": "https://www.drupal.org/files/issues/2022-01-19/batch-error-with-php-version-8-3259418-2.patch"
}
},
"drupal-scaffold": {
"locations": {
"web-root": "web/"
}
},
Alternativ hier der Inhalt der gepatchten Datei
bulk_user_registration/src/Form/BulkUserImport.php
:<?php
namespace Drupal\bulk_user_registration\Form;
use Drupal\bulk_user_registration\BulkUserRegistration;
use Drupal\bulk_user_registration\BulkUserRegistrationInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\FileBag;
/**
* Bulk user import form.
*
* @package Drupal\bulk_user_registration
*/
class BulkUserImport extends FormBase {
/**
* Request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The bulk user registration service.
*
* @var \Drupal\bulk_user_registration\BulkUserRegistration
*/
protected $bulkUserRegistration;
/**
* Constructor.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request stack.
* @param \Drupal\bulk_user_registration\BulkUserRegistration $bulkUserRegistration
* The bulk user registration service.
*/
public function __construct(RequestStack $requestStack, BulkUserRegistration $bulkUserRegistration) {
$this->requestStack = $requestStack;
$this->bulkUserRegistration = $bulkUserRegistration;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('request_stack'),
$container->get('bulk_user_registration')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bulk_user_registration_import';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['file_upload'] = [
'#type' => 'file',
'#title' => $this->t('Import CSV file'),
'#description' => $this->t('The CSV file to be imported. Check the CSV sample below if you are not sure about the format.'),
'#autoupload' => TRUE,
'#upload_validators' => ['file_validate_extensions' => ['csv']],
];
$form['sample_csv'] = [
'#type' => 'item',
'link' => [
'#type' => 'link',
'#title' => $this->t('Download sample CSV'),
'#url' => Url::fromRoute('bulk_user_registration.csv_sample'),
],
'#description' => $this->t('This sample file contains all possible fields and various data samples to get you started.'),
];
$form['default_role'] = [
'#type' => 'select',
'#title' => $this->t('Default role'),
'#description' => $this->t('The default role for imported users. When no role data is provided in the CSV, this role will be assigned.'),
'#options' => self::getAllowedRoles(),
'#default_value' => '',
'#required' => TRUE,
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Import'),
'#button_type' => 'primary',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// TODO Prevent form submit when file_upload is empty.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$defaultRole = $form_state->getValue('default_role');
$this->handleFileData($this->requestStack->getCurrentRequest()->files, $defaultRole);
}
/**
* To import data as users.
*
* @param \Symfony\Component\HttpFoundation\FileBag $filedata
* Field data.
* @param string $defaultRole
* The default role.
*/
protected function handleFileData(FileBag $filedata, $defaultRole) {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile */
$uploadedFiles = $filedata->get('files');
$location = $uploadedFiles['file_upload']->getRealPath();
if (($handle = fopen($location, 'r')) === FALSE) {
return;
}
// Read the csv data.
$headerData = [];
$csvData = [];
while (($data = fgetcsv($handle)) !== FALSE) {
if (empty($headerData)) {
$headerData = $data;
}
else {
$csvData[] = $data;
}
}
fclose($handle);
// Only standard and extra fields are allowed as csv columns. Unknown
// fields will be ignored.
$fieldNames = $this->bulkUserRegistration->getFieldNames();
$columnsToIgnore = [];
foreach ($headerData as $column => $header) {
if (!isset($fieldNames[$header])) {
$columnsToIgnore[] = $column;
}
}
// Collect the user data in a structured array. Where keys are the
// names of the appropriate header column.
$userData = [];
foreach ($csvData as $csvRow) {
$row_data = [];
foreach ($csvRow as $column => $value) {
if (in_array($column, $columnsToIgnore)) {
continue;
}
$row_data[$headerData[$column]] = trim($value);
}
$userData[] = $row_data;
}
$userData = array_filter($userData);
$this->batchProcessUserInfo($userData, $defaultRole);
}
/**
* Process user information in a batch.
*
* @param array $userData
* Structured array of user data.
* @param string $defaultRole
* Default user role.
*/
public function batchProcessUserInfo(array $userData, $defaultRole) {
$operations = [];
foreach ($userData as $data) {
$operations[] = [
'\Drupal\bulk_user_registration\Form\BulkUserImport::batchImport',
[
$data,
$defaultRole,
],
];
}
$batch = [
'title' => $this->t('Importing users..'),
'operations' => $operations,
'finished' => '\Drupal\bulk_user_registration\Form\BulkUserImport::batchFinished',
];
batch_set($batch);
}
/**
* Batch callback: User import operation.
*
* @param array $userData
* Structured array of user data. The keys are user field names.
* @param string $defaultRole
* The default role.
* @param array $context
* Batch context data.
*/
public static function batchImport(array $userData, $defaultRole, array &$context) {
// Required user data is missing. Do not import.
if (empty($userData[BulkUserRegistrationInterface::FIELD_EMAIL]) || empty($userData[BulkUserRegistrationInterface::FIELD_USER_NAME])) {
return;
}
// This user already exists. Do not import.
if (user_load_by_mail($userData[BulkUserRegistrationInterface::FIELD_EMAIL])) {
return;
}
$user = \Drupal::service('bulk_user_registration')
->createUser($userData, $defaultRole);
// Notify user via mail.
if ($user->isActive()) {
_user_mail_notify('register_no_approval_required', $user);
}
$context['results'][] = $user->id();
}
/**
* Batch callback: Finish bulk user import process.
*
* @param bool $success
* Success or not.
* @param array $results
* Results array.
* @param mixed $operations
* Operations.
*/
public static function batchFinished($success, array $results, $operations) {
$messenger = \Drupal::messenger();
if ($success) {
$messenger->addStatus(\Drupal::translation()
->formatPlural(count($results), '1 user imported.', '@count users imported.'));
}
else {
$messenger->addError(t('Finished with errors.'));
}
}
/**
* The roles allowed to import.
*
* @return array
* An associative array with the role id as the key and the role name as
* value.
*/
protected function getAllowedRoles() {
$allowedRoles = \Drupal::config('bulk_user_registration.settings')->get('allowed_roles');
return array_intersect_key(user_role_names(TRUE), array_flip(array_filter($allowedRoles)));
}
}
Also wenn Composer dir sagt.
am 19.02.2022 - 11:34 Uhr
Also wenn Composer dir sagt. Hi ich kann den Patch nicht anwenden. Dann ist doch die Frage warum nicht. Ich mach das, da Ann immer mit git apply und lass mir, den output ausgeben, damit ich sehe, warum der Patch nicht angewendet werden kann.
Meistens ist er dann schon in Der aktuellen release verbaut.
Aber du musst halt erst mal rauskriegen ob du den Patch überhaupt noch applyen kannst. Wenn nicht schmeiß ihn aus, der Composer. Json. Wenn ja check deiner Composer.json noch mal.
https://drupal-tv.de
Drupal sehen und lernen
dinmikkith schrieb Also wenn
am 19.02.2022 - 14:37 Uhr
Also wenn Composer dir sagt. Hi ich kann den Patch nicht anwenden. Dann ist doch die Frage warum nicht. Ich mach das, da Ann immer mit git apply und lass mir, den output ausgeben, damit ich sehe, warum der Patch nicht angewendet werden kann.
Meistens ist er dann schon in Der aktuellen release verbaut.
Aber du musst halt erst mal rauskriegen ob du den Patch überhaupt noch applyen kannst. Wenn nicht schmeiß ihn aus, der Composer. Json. Wenn ja check deiner Composer.json noch mal.
Wie ich oben bereits geschrieben habe, ist in der composer.json fälschlicherweise drupal/core als Patch-Ziel angegeben anstatt von drupal/bulk_user_registration.
https://duvien.com/blog/how-a
am 19.02.2022 - 18:54 Uhr
https://duvien.com/blog/how-apply-patch-file-composer-based-drupal-89
https://drupal-tv.de
Drupal sehen und lernen