You are here

rlayout.module in Layout 8

Responsive layout builder tool for Panels.

File

rlayout.module
View source
<?php

/**
 * @file
 * Responsive layout builder tool for Panels.
 */
use Drupal\rlayout\RLayout;

/**
 * Implements hook_menu().
 */
function rlayout_menu() {
  $items = array();
  $items['admin/structure/layouts'] = array(
    'title' => 'Layouts',
    'description' => 'Manage list of layouts.',
    'page callback' => 'rlayout_page_list',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer layouts',
    ),
    'file' => 'rlayout.admin.inc',
  );
  $items['admin/structure/layouts/add'] = array(
    'title' => 'Add layout',
    'page callback' => 'rlayout_page_add',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer layouts',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'rlayout.admin.inc',
  );
  $items['admin/structure/layouts/manage/%rlayout'] = array(
    'title' => 'Edit layout',
    'page callback' => 'rlayout_page_edit',
    'page arguments' => array(
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer layouts',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'rlayout.admin.inc',
  );
  $items['admin/structure/layouts/manage/%rlayout/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/structure/layouts/manage/%rlayout/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'rlayout_delete_confirm',
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer layouts',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'rlayout.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function rlayout_permission() {
  return array(
    'administer layouts' => array(
      'title' => t('Administer responsive layouts'),
      'description' => t('Administer backend settings for responsive layouts.'),
    ),
  );
}

/**
 * Implements hook_entity_info().
 */
function rlayout_entity_info() {
  $types['rlayout'] = array(
    'label' => 'Responsive layout',
    'entity class' => 'Drupal\\rlayout\\RLayout',
    'controller class' => 'Drupal\\Core\\Config\\Entity\\ConfigStorageController',
    'form controller class' => array(
      'default' => 'Drupal\\rlayout\\RLayoutFormController',
    ),
    'list controller class' => 'Drupal\\Core\\Config\\Entity\\ConfigEntityListController',
    'list path' => 'admin/structure/layouts',
    'uri callback' => 'rlayout_uri',
    'config prefix' => 'rlayoutset',
    'entity keys' => array(
      'id' => 'id',
      'label' => 'label',
      'uuid' => 'uuid',
    ),
  );
  return $types;
}

/**
 * Entity URI callback.
 *
 * @param Drupal\rlayout\RLayout $layout
 *   Layout configuration entity instance.
 *
 * @return array
 *   Entity URI information.
 */
function rlayout_uri(RLayout $layout) {
  return array(
    'path' => 'admin/structure/layouts/manage/' . $layout
      ->id(),
  );
}

/**
 * Load one layout object by its identifier.
 *
 * @return Drupal\rlayout\RLayout
 *   Layout configuration entity instance.
 */
function rlayout_load($id) {
  return entity_load('rlayout', $id);
}

/**
 * Load all layout objects.
 *
 * @return array
 *   List of Drupal\rlayout\RLayout instances keyed by id.
 */
function rlayout_load_all() {
  return entity_load_multiple('rlayout');
}

/**
 * Helper function to return processed breakpoint information for layouts.
 *
 * @todo
 *   Currently tied to one breakpoint group in rlayout, but it should be
 *   made independent and configurable.
 */
function rlayout_breakpoints_load_all() {
  $grid_breakpoints = entity_load('breakpoint_group', 'module.rlayout.rlayout');
  $breakpoint_info = array();
  foreach ($grid_breakpoints->breakpoints as $key => $breakpoint) {

    // Only include this breakpoint in the output if we found a grid for it.
    // Other type of breakpoints are not useful for us, since we cannot display
    // an editing interface without a grid for each breakpoint.
    if ($grid = rlayout_breakpoint_find_grid($key)) {

      // @todo This considers em and px based widths the same number. This is due
      // to the JS responsive layout designer not being able to take qualified
      // widths. It can only take numbers. Should be fixed there first and then
      // here.
      $low_width = 0;
      if (preg_match('!min-width: (\\d+)[ep]!', $breakpoint->mediaQuery, $found)) {
        $low_width = $found[1];
      }
      $breakpoint_info[$key] = (object) array(
        'id' => $key,
        'label' => $breakpoint->label,
        'width' => $low_width,
        'grid' => $grid,
        'mediaQuery' => $breakpoint->mediaQuery,
      );
    }
  }
  return $breakpoint_info;
}

/**
 * Find the (first) grid matching this breakpoint.
 */
function rlayout_breakpoint_find_grid($breakpoint_key) {
  $all_grids = entity_load_multiple('grid');
  foreach ($all_grids as $grid) {
    if (!empty($grid->options['breakpoints']) && in_array($breakpoint_key, $grid->options['breakpoints'])) {
      return $grid
        ->id();
    }
  }
  return FALSE;
}

/**
 * Implements hook_library_info().
 */
function rlayout_library_info() {
  $path = drupal_get_path('module', 'rlayout');
  $rld_path = $path . '/designer';
  $libraries['rlayout-designer'] = array(
    'title' => 'Responsive layout designer',
    'version' => '0.1',
    'js' => array(
      $rld_path . '/assets/js/plugins/breakup/jquery.breakup.js' => array(),
      $rld_path . '/app/main.js' => array(),
      $rld_path . '/app/libs/Utils/Utils.js' => array(),
      $rld_path . '/app/libs/LayoutManager/LayoutManager.js' => array(),
      $rld_path . '/app/libs/LayoutPreviewer/LayoutPreviewer.js' => array(),
      $rld_path . '/app/libs/LayoutList/LayoutList.js' => array(),
      $rld_path . '/app/libs/LayoutStep/LayoutStep.js' => array(),
      $rld_path . '/app/libs/StepManager/StepManager.js' => array(),
      $rld_path . '/app/libs/StepList/StepList.js' => array(),
      $rld_path . '/app/libs/Step/Step.js' => array(),
      $rld_path . '/app/libs/RegionList/RegionList.js' => array(),
      $rld_path . '/app/libs/Region/Region.js' => array(),
      $rld_path . '/app/libs/GridList/GridList.js' => array(),
      $rld_path . '/app/libs/Grid/Grid.js' => array(),
    ),
    'css' => array(
      $rld_path . '/assets/css/application.css' => array(),
      $rld_path . '/assets/css/grid.css' => array(),
    ),
  );
  $libraries['rlayout-admin'] = array(
    'title' => 'Layout admin interface',
    'version' => '0.1',
    'js' => array(
      $path . '/rlayout-admin.js' => array(),
    ),
    'css' => array(
      $path . '/rlayout-admin.css' => array(),
    ),
  );
  return $libraries;
}

/**
 * Build CSS for the breakpoints with media queries.
 *
 * @param boolean $include_media_queries
 *   Whether generate one flat CSS without media queries (useful for
 *   administration), or wrap breakpoints with media queries (for frontend).
 *
 * @todo
 *   Figure out a good way to avoid equal max/min-weights in subsequent
 *   breakpoints if that is a problem.
 */
function rlayout_breakpoint_get_css($include_media_queries = TRUE) {
  $breakpoints = rlayout_breakpoints_load_all();
  $breakpoint_css = array();
  foreach ($breakpoints as $name => $breakpoint) {
    $grid = entity_load('grid', $breakpoint->grid);
    if ($include_media_queries) {
      $breakpoint_css[] = '@media ' . $breakpoint->mediaQuery . ' {';

      // Get grid CSS from gridbuilder and apply some extra indentation.
      $breakpoint_css[] = '  ' . str_replace("\n", "\n  ", $grid
        ->getGridCss('.panel-responsive', '.rld-span-' . $name . '_'));
      $breakpoint_css[] = "\n}";
    }
    else {
      $breakpoint_css[] = $grid
        ->getGridCss(NULL, NULL, TRUE);
    }
  }
  $css = join("\n", $breakpoint_css);
  return $css;
}

Functions

Namesort descending Description
rlayout_breakpoints_load_all Helper function to return processed breakpoint information for layouts.
rlayout_breakpoint_find_grid Find the (first) grid matching this breakpoint.
rlayout_breakpoint_get_css Build CSS for the breakpoints with media queries.
rlayout_entity_info Implements hook_entity_info().
rlayout_library_info Implements hook_library_info().
rlayout_load Load one layout object by its identifier.
rlayout_load_all Load all layout objects.
rlayout_menu Implements hook_menu().
rlayout_permission Implements hook_permission().
rlayout_uri Entity URI callback.