Drupal 8 : Get title and meta tags from Site URL using Ajax

This CodeLet set title and body field values using Ajax.
Place this CodeLet to your custom module file(.module) or Create a Custom module for Drupal 8 as per documentation https://www.drupal.org/docs/8/creating-custom-modules.

First of all add a field(field_link) to your content type. after that add an ajax to that field as per CodeLet.
Ajax example to replace multiple fields on ajaxCallback.
You set multiple fields value on ajax trigger in Drupal 8.

CodeLet: 

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
// Add above lines in module file
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'FORM_ID') {//FORM_ID = node_TYPE_form
//Add prefix and suffix to title field for ajax response.
$form['title'][''] = '';
$form['title'][''] = '';
//Add prefix and suffix to body field for ajax response.
$form['body'][''] = '';
$form['body'][''] = '';
$form['field_link']['widget'][0]['value'][''] = array(
'callback' => 'MYMODULE_LinkCallback',//Ajax callback call on ajax
'event' =>'change',
'wrapper' => 'MYMODULE-link-wrapper',
'suppress_required_fields_validation' => TRUE,
'progress' => array(
// Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.
'type' => 'throbber',
// Message to show along progress graphic. Default: 'Please wait...'.
'message' => NULL,
),
);
}
}

/**
* Ajax callback
* MYMODULE_LinkCallback
*
* @param array $form
* @param FormStateInterface $form_state
* @return Ajax response
*/
function MYMODULE_LinkCallback(array $form, $form_state) {
$link_detail = '';
$title = 'Enter a title';
$description = 'Enter a summary';
$link = '';

if ($form_state->getValue('field_link')) {//get input url
$link = $form_state->getValue('field_link');
$link = $link['0']['value'];
}
if ($link) {
$parsed = parse_url($link);
if (empty($parsed['scheme'])) {
$link = 'http://' . ltrim($link, '/');
}
$link_data = _GetUrlData($link);

$title = isset($link_data['title']) ? $link_data['title'] : '';
$description = isset($link_data['description']) ? $link_data['description'] : '';
//set Title and description value
$form['title']['widget'][0]['value'][''] = $title;
$form['body']['widget'][0]['value'][''] = $description;
}

$ajax_response = new AjaxResponse();

//Replace Title and Description
$ajax_response->addCommand(new ReplaceCommand('-title-wrapper', $article_form['body']));
$ajax_response->addCommand(new ReplaceCommand('-title-wrapper', $article_form['title']));

return $ajax_response;
}

/**
* _GetUrlData
* @param string $url
* @return Ambigous
*/
function _GetUrlData($url = '') {
$result = false;
if (!empty($url))
$contents = @file_get_contents($url);

if (isset($contents) && is_string($contents)) {
$title = null;
$metaTags = null;

preg_match('/([^>]*)<\/title>/si', $contents, $match );

if (isset($match) && is_array($match) && count($match) > 0) {
$title = strip_tags($match[1]);
}

preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);

if (isset($match) && is_array($match) && count($match) == 3) {
$originals = $match[0];
$names = $match[1];
$values = $match[2];
if (count($originals) == count($names) && count($names) == count($values)) {
for ($i=0, $limiti=count($names); $i < $limiti; $i++) {
$result[strtolower($names[$i])] = $values[$i];
}
}
}
$result['title'] = $title;
}

return $result;
}

By saru1683 on Sat, 04/02/2017 - 11:42am

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback