Drupal StackExchange

"Missing class doc comment" when using attribute on class

7 hours 38 minutes ago

I recently updated PHP from 8.1 to 8.2.

This resulted in a bunch of deprecation notices about use of dynamic properties in PHPUnit tests.

So, based on the docs, I added the AllowDynamicProperties class.

/** * Unit tests for the myModule class (timing). * * @group myModuleSuite * * @coversDefaultClass \Drupal\myModule\Class * * @property \DateTimeZone $timezone_utc * @property \DateTimeZone $timezone_nyc */ #[\AllowDynamicProperties] class MyModuleTimingTest extends UnitTestCase {

This fixes the deprecation notice for PHP 8.2, but now the Drupal coding standards check complains about a "missing doc comment" for my MyModuleTimingTest class.

How am I supposed to write this so that I can use the attribute and not violate the coding standards?

How I'm checking the coding standards

Using Coder module 8.3.16.

/vendor/bin/phpcs -p --colors --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md --ignore=node_modules,vendor ./web/modules/custom ./web/themes/custom

Patrick Kenny

How do I include a list of nodes in Webform, with multiple fields per node / views row?

8 hours 14 minutes ago

In a Drupal 7 project, I had used the webform_view (https://www.drupal.org/project/webform_view) module to list a number of nodes in a webform with a "quantity" and "size" field for each node/row to create a simple product order form. The form structure looks something like this...

[Node 1 description (from view)] [Node 1 quantity number field] [Node 1 size select field (options drawn from node content)] [Node 2 description (from view)] [Node 2 quantity field] [Node 2 size select field (options drawn from node content)] ...

The time has come to re-create the site in Drupal 9, and it doesn't appear any similar module exists for D8/9. Any ideas on how to achieve this functionality? I'm trying to avoid using Drupal Commerce for such a simple order form.


Stuart Layland

How to change background image based on current node?

8 hours 18 minutes ago

I am trying to change the header background image of my website based on the current node. I have added an image field to my content type, field_hero_image, and, in preprocess function mytheme_preprocess_page(&$variables), I get the image url and try to inject it in the page, as inline css:

function mytheme_preprocess_page(&$variables) { $node = $variables['node']; if ($node) { $path = file_create_url($node->field_hero_image->entity->getFileUri()); $variables['page']['#attached']['css'] = array( 'data' => '.header-wrapper { background:url(' . $path . ') center center no-repeat !important; background-size: cover;}', 'type' => 'inline', ); } }


function mytheme_preprocess_page(&$variables) { $node = $variables['node']; if ($node) { $path = file_create_url($node->field_hero_image->entity->getFileUri()); $variables['#attached']['library'][] = array( 'data' => '.header-wrapper { background:url(' . $path . ') center center no-repeat !important; background-size: cover;}', 'type' => 'inline', ); } }

None of this works.

Is there a way to inject css from the preprocess function? I know that I can get the path value, put it in $variables and use it in twig. But this doesn't seem a clean solution to me.

Oana Hulpoi

How can I get a default value in a dropdown using form API?

9 hours 25 minutes ago
$results = db_select('users', 'u')->fields('u', ['name', 'uid', 'mail'])->execute(); $options = []; foreach ($results as $result) { $options[$result->uid] = $result->name . ' | ' . $result->mail; } unset($options[array_search(0, $options)]); $form['user'] = [ '#type' => 'select', '#title' => t('Select User'), '#options' => $options, '#default_value' => ('select a value'), '#states' => [ 'visible' => [ ':input[name="data_set"]' => ['value' => 'education'], ], ], ]; $form['submit'] = [ '#type' => 'submit', '#value' => t('Expire'), ];

I have this dropdown , how can I get the default value like Select a value or something


Why Content Path cannot be used to sort a view?

10 hours 20 minutes ago

Using Views with Drupal 7, how can I use the content path to sort a result table?

I have node title, created date and path as fields for a view. The first two fields have the checkbox to make them sorting options in the display settings, but the node path field doesn't.


Disable user language setting in user-edit page

10 hours 29 minutes ago

I want to unset this specific part of the settings form in template php. I found some code example for Drupal 6, but can't figure out how to do it in Drupal 7.

I tried this without result:

if ($form_id == 'user_profile_form') { unset($form['language']); }

(There is a module "user tweaks" but I want it to do it in the preprocess)


How to get the current running Drush command?

11 hours 12 minutes ago

I would like to detect from code the current running drush command. I have some hooks that will be run when I exec some drush commands and want to detect which command is triggering the hook.

I manage to get the info from input() but I want to know if there is a more appropriate way:

if (PHP_SAPI === 'cli') { $command = \Drush\Drush::input()->getArguments() }

I would like to have something similar to this:

if (PHP_SAPI === 'cli') { $command = \Drush\Drush::...getCurentCommand(); }

Similar to Drupal::routeMatch()->getRouteName() in someway.

Thank you


How to get the parameters from a URL and a route that is not the current one?

11 hours 13 minutes ago

Route from a custom module :

custom.route: path: /the/{id}/path

Hypothetical url : /the/53/path

The goal is the get the id value ("53").

So far, I managed to get the route name from the url with

$url = "/the/53/path"; $route_provider = \Drupal::service('router.route_provider'); $found_routes = $route_provider->getRoutesByPattern($url);

In $found_routes, I can find the "custom.route" route name.

Now, I need to find the parameters value, but I can't achieve it.

use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; /** * Returns the parameters of a given URL. * * @param string $url * The URL. * @param string $route_name * The name of the route. * * @return array * An array of parameters for the URL. */ function getUrlParameters(string $url, string $route_name): array { // Create a URL object from the given URL string. $url_object = Url::fromUri('base:/' . ltrim($url, '/')); // Create a request object from the URL object. $request = \Drupal::requestStack()->getMasterRequest(); $request = $request->duplicate(); $request->setMethod('GET'); $request->attributes->set('_route', $route_name); $url_object->mergeOptions(['query' => $request->query->all()]); $request->server->set('REQUEST_URI', $url_object->toString()); // Use the route matcher to get the route matching the URL. /** @var \Drupal\Core\Routing\RouteMatchInterface $route_match */ $route_match = RouteMatch::createFromRequest($request, \Drupal::service('router')); // Return the parameters of the URL. return $route_match->getParameters()->all(); }