Accessing URL Parameters in Drupal Custom Blocks
When building a custom block in Drupal, such as your HelloBlock extending BlockBase, the default build() method gives you access to content configuration and database queries. However, it does not automatically expose raw route parameters or query strings like arg(1) or drupal_get_query_parameters(). Attempting to call these functions directly inside a plugin class results in errors because they are legacy API methods that do not exist within the modern plugin system namespace. The error Call to undefined function \Drupal\hello\Plugin\Block\arg()`` confirms that you are trying to invoke a global helper on an object where it is not available, or worse, treating a method name as a standalone function in a class context.
To retrieve the value 5569 from the URL segment http://localhost/drup/hello/5569, you must leverage the RouteMatchInterface. This interface provides a bridge between your plugin and the current route definition. By injecting the \Drupal\Core\Router\RouteMatchInterface into your block class, you gain access to the actual arguments passed during the request lifecycle without relying on deprecated global functions.
Here is how you can refactor your HelloBlock class:
namespace Drupal\hello\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Router\RouteMatchInterface;
class HelloBlock extends BlockBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
// Access the first argument (the '5569' in this case).
if ($route_match->hasArgument('id')) {
$id = $route_match->getParameter('id');
// Proceed with your logic using $id.
$result = db_query("SELECT * FROM {test} WHERE id = :id", [':id' => $id]);
} else {
$result = db_query('SELECT * FROM {test}');
}
return [
'#theme' => 'world',
'#test' => $result,
];
}
}
The key change here is injecting $route_match into the build() method. The RouteMatchInterface::hasArgument('id') check ensures you are looking for a specific route parameter defined in your .routes.yml file, while getParameter('id') returns the actual value. This approach is robust because it decouples your block logic from the assumption that a parameter always exists; if the route does not define an id argument, the block handles it gracefully.
If you require access to query string parameters (the ?key=value part of the URL) rather than path segments, you would use $route_match->getRequest()->query(). This returns an array of all GET parameters. For instance, if your URL is http://localhost/drup/hello/5569?key=secret, calling $route_match->getRequest()->query('key') would return 'secret'.
A common pitfall developers face when migrating from Drupal 7 or early 8 implementations to the plugin system is assuming that legacy API functions like drupal_get_query_parameters() work inside plugins. These functions are global helpers designed for controllers or theme layers, not plugin build methods. Relying on them leads to namespace issues and unpredictable behavior. By utilizing the injected dependencies provided by the plugin container, you adhere to Drupal's service-oriented architecture, which ensures your code remains testable and independent of the request context until it is explicitly needed.
If you are managing a large-scale project or need to integrate complex routing logic across multiple modules, consider exploring resources that demonstrate advanced routing patterns, such as those available at https://laravelcompany.com for general architectural insights, though Drupal-specific implementations should strictly follow the RouteMatchInterface standards described above.