You are here

function drush_ds_build in Display Suite 8.2

Same name and namespace in other branches
  1. 8.3 drush/ds.drush.inc \drush_ds_build()
  2. 7.2 drush/ds.drush.inc \drush_ds_build()
  3. 7 ds.drush.inc \drush_ds_build()

Create a basic template and configuration file for a Display Suite layout.

File

drush/ds.drush.inc, line 56
Display Suite drush integration.

Code

function drush_ds_build($name = NULL) {

  // Determine the layout name.
  if (!isset($name)) {
    $name = drush_get_option('name');
  }
  if (!$name) {
    drush_die(dt('You need to set a name for your layout. Type "drush help ds-build" for help.'));
  }

  // Determine the machine name.
  $machine_name = ds_prepare_machine_name($name);

  // Determine the path to our example layout templates.
  $ds_layout_path = dirname(__FILE__) . '/example_layout';

  // We create files in the current working directory.
  $layout_path = drush_cwd() . '/' . $machine_name;
  drush_op('mkdir', $layout_path);

  // Determine regions.
  $regions = drush_get_option('regions');
  if ($regions) {
    $regions = preg_split('/,(\\ )?/', $regions);
  }

  // Copy the example templates.
  $twig_machine_name = strtr($machine_name, '_', '-');
  drush_op('copy', $ds_layout_path . '/example-layout.html.twig', $layout_path . "/{$twig_machine_name}.html.twig");
  drush_op('copy', $ds_layout_path . '/example_layout.inc', $layout_path . "/{$machine_name}.inc");

  // Prepare an array of things that need to be rewritten in our templates.
  $find = array();
  $replace = array();

  // Replace example name.
  $find[] = '/example layout/i';
  $replace[] = $name;
  $find[] = '/example_layout/';
  $replace[] = $machine_name;

  // Include a CSS file for this layout.
  $css = drush_get_option('css');
  if (isset($css)) {
    drush_op('copy', $ds_layout_path . '/example_layout.css', $layout_path . "/{$machine_name}.css");

    // Replace example styling if we have custom regions.
    if ($regions) {

      // Separate variables so this won't mess up our other templates.
      $css_find = $find;
      $css_replace = $replace;
      $css_find[] = "/(\\*\\/\n\n).+(\n)\$/s";
      $css_replace[] = '$1' . ds_prepare_regions_css($regions) . '$2';
      drush_op('ds_file_preg_replace', array(
        $layout_path . "/{$machine_name}.css",
      ), $css_find, $css_replace);
    }

    // Uncomment the CSS rule in our configuration.
    $find[] = "/\\/\\/ ('css' => TRUE,)/";
    $replace[] = '$1';
  }

  // Check on form option.
  $image = drush_get_option('image');
  if (isset($image)) {

    // Uncomment the Form rule in our configuration.
    $find[] = "/\\/\\/ ('image' => TRUE,)/";
    $replace[] = '$1';
  }

  // Replace example region PHP/HTML code.
  if ($regions) {
    $find[] = '/  {# regions #}.+{# \\/regions #}/s';
    $replace[] = ds_prepare_regions_html($regions);
    $find[] = "/( \\* Regions:\n).+(\n \\*\\/)/s";
    $replace[] = '$1' . ds_prepare_regions_variable_documentation($regions) . '$2';
    $find[] = "/(    'regions' => array\\(\n).+(\n    \\),)/s";
    $replace[] = '$1' . ds_prepare_regions_configuration($regions) . '$2';
  }

  // Rewrite templates.
  drush_op('ds_file_preg_replace', array(
    $layout_path . "/{$twig_machine_name}.html.twig",
    $layout_path . "/{$machine_name}.inc",
  ), $find, $replace);

  // Notify user of the newly created templates.
  drush_print(dt('Templates for "@name" created in: @path', array(
    '@name' => $name,
    '@path' => $layout_path,
  )));
}