Sunday, 13 October 2013

Drupal7 customizing theme part.2

To apply the body width which a user enter, I modified the code in  page.tpl.php
-------------------------------------------------------------------------------------------------------------------
<div class="page-wrapper" style="<?php print theme_get_setting('body_width'); ?>"><div id="page">
-------------------------------------------------------------------------------------------------------------------
I should show the code for validate the width value.
-------------------------------------------------------------------------------------------------------------------
<theme-settings.php>

function holiday_body_width_validate($element, &$form_state){
  if(!empty($element['#value'])){
    $width = $element['#value'];

    if(preg_match("/(\d+)\s*%/", $width, $match)) {  // Perform a regular expression match
      $num = intval($match[0]);
      if(50 <= $num && $num <= 100) {
        return;
      }
      else {
        form_error($element, t('The value of body width must be between 50% and 100%'));
      }
    }
    else if(preg_match("/(\d+)\s*px/", $width, $match)){
      $num = intval($match[0]);
      if(800 <= $num && $num <= 1600) {
        return;
      }
      else {
        form_error($element, t('The value of body width must be between 800px and 1600px'));
      }
    }
  }
-------------------------------------------------------------------------------------------------------------------
If you enter the value less than 50%(or 800px) or more than 100%(1600px), you will see the following error message.


 

No comments:

Post a Comment