You are here

function freelinking_drupaldev_callback in Freelinking 7.3

Builds a link to a node in Drupal.org.

Return value

array with link details.

1 string reference to 'freelinking_drupaldev_callback'
freelinking_dev.inc in plugins/freelinking_dev.inc

File

plugins/freelinking_dev.inc, line 36

Code

function freelinking_drupaldev_callback($target, $plugin) {
  if ('/^d(rupal)?project/' == $plugin['indicator']) {
    $message = t('Drupal.org/project/@project not found', array(
      '@project' => $target['dest'],
    ));
  }
  else {
    if (!is_numeric($target['dest'])) {
      return array(
        'failover' => 'error',
        'message' => t('Invalid node id @nid', array(
          '@nid' => $target['dest'],
        )),
      );
    }
    $message = t('Drupal.org/node/@nid not found', array(
      '@nid' => $target['dest'],
    ));
  }
  $scrape = !variable_get('freelinking_external_http_request', TRUE);
  $url = check_url(preg_replace('/%1/', $target['dest'], $plugin['replacement']));
  if ($scrape) {
    $result = drupal_http_request($url, array(
      'headers' => array(),
    ));
    if (!$result->code || 400 <= $result->code) {
      return array(
        'failover' => 'error',
        'message' => $message,
      );
    }
  }
  $tooltip = $target['tooltip'] ? $target['tooltip'] : $plugin['tip'];
  if (!$target['text'] && $scrape) {
    $found_title = preg_match('/<h1.*>(.*)<\\/h1>/', $result->data, $matches);
    if ($found_title) {
      if (strlen($matches[1]) < 3) {
        $found_title = preg_match('/<h2.*>(.*)<\\/h2>/', $result->data, $matches);
      }
    }
    if ($found_title) {

      // regex to scrape title from page worked.
      $replacement = array(
        t('Drupal.org: “@title”', array(
          '@title' => $matches[1],
        )),
        check_url($url),
        array(
          'attributes' => array(
            'title' => $tooltip,
          ),
        ),
      );
    }
  }
  elseif ($target['text']) {
    $replacement = array(
      check_plain($target['text']),
      check_url($url),
      array(
        'attributes' => array(
          'title' => $tooltip,
        ),
      ),
    );
  }
  if (empty($replacement)) {
    $prefix = is_numeric($target['dest']) ? '#' : '';
    $replacement = array(
      t('Drupal.org: “!prefix@title”', array(
        '!prefix' => $prefix,
        '@title' => $target['dest'],
      )),
      check_url($url),
      array(
        'attributes' => array(
          'title' => $tooltip,
        ),
      ),
    );
  }
  return $replacement;
}