You are here

record_shorten.module in Shorten URLs 8

Same filename and directory in other branches
  1. 8.2 modules/record_shorten/record_shorten.module

Records shortened URLs.

File

modules/record_shorten/record_shorten.module
View source
<?php

/**
 * @file
 *   Records shortened URLs.
 */

/**
 * Implements hook_theme().
 */
function record_shorten_theme($existing, $type, $theme, $path) {
  return array(
    'record_shorten_records' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_shorten_create().
 */
function record_shorten_shorten_create($old, $new, $service) {
  $array = array(
    'original' => $old,
    'short' => $new,
    'service' => $service,
    'uid' => \Drupal::currentUser()
      ->id(),
    'hostname' => \Drupal::request()
      ->getClientIp(),
    'created' => REQUEST_TIME,
  );

  // @TODO : This creates duplicate records. Needs to be fixed.
  \Drupal::database()
    ->merge('record_shorten')
    ->key($array)
    ->fields($array)
    ->execute();
}

/**
 * Builds a list of shortened URLs.
 */
function record_shorten_records_table() {

  // @TODO : Views display plugin needs to be fixed.
  // if (\Drupal::moduleHandler()->moduleExists('views')) {
  //   return views_embed_view('record_shorten', 'default');
  // }
  $header = array(
    t('Original'),
    t('Short'),
    t('Service'),
  );
  $rows = array();

  // SELECT original, short, service FROM {record_shorten} ORDER BY sid DESC
  $result = db_select('record_shorten', 'rs')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->limit(10)
    ->fields('rs', array(
    'original',
    'short',
    'service',
  ))
    ->orderBy('rs.sid', 'DESC')
    ->execute();
  foreach ($result as $row) {

    // Sigh... DBTNG doesn't have a ->fetchAsNonAssocArray()
    $rows[] = array(
      \Drupal\Component\Utility\Html::escape($row->original),
      \Drupal\Component\Utility\Html::escape($row->short),
      \Drupal\Component\Utility\Html::escape($row->service),
    );
  }
  $table = array(
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );

  // Render Table.
  $output = drupal_render($table);

  // Finally add Pager.
  $pager = array(
    '#type' => 'pager',
  );
  $output .= drupal_render($pager);
  return $output;
}

/**
 * Implements hook_views_api().
 */
function record_shorten_views_api() {
  return array(
    'api' => 3,
  );
}

Functions

Namesort descending Description
record_shorten_records_table Builds a list of shortened URLs.
record_shorten_shorten_create Implements hook_shorten_create().
record_shorten_theme Implements hook_theme().
record_shorten_views_api Implements hook_views_api().