> For the complete documentation index, see [llms.txt](https://drupal.bermaki.com/drupal-documentation-and-guides/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://drupal.bermaki.com/drupal-documentation-and-guides/backend/helpful-drupal-classes-and-methods.md).

# Helpful Drupal Classes and Methods

These are some of the Drupal core classes and their methods that help me in my day to day work.

<details>

<summary>Drupal Core Class Methods</summary>

### Drupal::Url

#### Get current Url

```php
$url = Url::fromRoute('<current>'); //Route Object, if no options were added we get a relative URL
$url = Url::fromRoute('<current>',['absolute'=> 'true']); //Route Object, 'Absolute' parameter was passed, we get a an absolute url
$url = $url->toString(); // String url
```

#### Pass Parameters to another Route

```php
// On form submit, do this
 $url = Url::fromRoute('ROUTE')->setRouteParameters([
    'name' => $name,
]);

// redirect form to this url with these parameters
$form_state->setRedirectUrl($url);
```

### Drupal::messenger

Drupal::messenger() helps us show messages on the page

#### Create a link as a message

```php
   \Drupal::messenger()->addError('');
   \Drupal::messenger()->addWarning('');
   \Drupal::messenger()->addStatus(['#markup' => '<a href=' . $enlace_convocatoria_nueva . ' target="_blank" rel= noopener noreferrer" >OPEN LINK</a>']);
```

### Drupal::routeMatch

The `Drupal::routeMatch()` method is used to get the current route and its parameters.

#### Get current Route

```php
$current_route = \Drupal::routeMatch()->getRouteName();
$current_parameters = \Drupal::routeMatch()->getParameters();
```

### Drupal::request

The `Drupal::request()` method is used to get the current request and its parameters.

#### Get current domain

```php
\Drupal::request()->getSchemeAndHttpHost()
```

#### Get BaseUrl

```php
\Drupal::request()->baseUrl()
```

#### Get a specific query parameters from current URL

```php
$keyword = \Drupal::request()->query->get('QUERY_PARAMETER');
```

</details>

<details>

<summary>Services</summary>

The Drupal::service() method is used to get a service from the container. Services are objects that are used to perform a specific task. For example, the `path.alias_manager` service is used to get the alias of a path. The container is a registry of all the services that are available in Drupal. You can get a list of all the services by going to `http://localhost/my_site_name_dir/web/core/modules/system/src/ServiceProviders/ServiceProvider.php`.

#### path.current - get current path

```php
$current_path = \Drupal::service('path.current')->getPath();
```

#### path\_alias.manager - get current path alias

```php
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
```

path.matcher

The `path.matcher` service is used to check if the current path is the frontpage.

```php
 \Drupal::service('path.matcher')->isFrontPage();
```

</details>
