drush eval "print_r(\Drupal::service('smart_link')->getUrlInfo($url))"
Services are loaded when necessary and easy to access in other modules.
To turn the previous code into a service, first we need to set up a services file.
smart_link.services.yml
services:
smart_link:
class: Drupal\smart_link\SmartLink
Then we need the service class.
smart_link/src/SmartLink.php
<?php
namespace Drupal\smart_link;
use Masterminds\HTML5;
/**
* SmartLink service.
*/
class SmartLink {
/**
* The Masterminds/HTML5 library.
*
* @var HTML5
*/
protected $html5;
/**
* Constructs a SmartLinkService object.
*/
public function __construct() {
$this->html5 = new HTML5();
}
/**
* Get URL info.
*/
public function getUrlInfo($url) {
$page = $this->html5->load($url);
$meta = [];
$description = '';
foreach ($page->getElementsByTagName('meta') as $meta_element) {
if ($meta_element->getAttribute('name') == 'description') {
$description = $meta_element->getAttribute('content');
}
$meta[] = simplexml_import_dom($meta_element)->asXML();
}
return [
'domain' => parse_url($url)['host'],
'path' => parse_url($url)['path'],
'description' => $description,
'title' => $page->getElementsByTagName('title')[0]->textContent,
'h1' => $page->getElementsByTagName('h1')[0]->textContent,
'meta' => implode("\n", $meta),
];
}
}
Call the function from drush
drush eval "print_r(\Drupal::service('smart_link')->getUrlInfo($url))"
Topics