Search code examples
phpdrupaldrupal-7

How to manage page--front.tpl.php images from back-end/admin-panel in drupal 7


I am working on a website in drupal 7, I have created a Home page in page--front.tpl.php with Static Html like the code below,

<div class="col-sm-8 col-xs-24 botmboxes">
              <div class="row">
                <div class="boxbotmimg">
                  <img class="img-responsive" src="./sites/all/themes/korhanifashion/images/PlasticRug-Boat.png">
                  <span class="hovblck"></span>
                </div>
                <div class="botboxhov">
                  <a href="<?php print $front_page; ?>limited_offers">
                    <img class="bbres" src="./sites/all/themes/korhanifashion/images/limroffer.png"></a>
                </div>
              </div>
            </div>
            <div class="col-sm-4 col-xs-24 botmboxes botboxthree">
              <div class="row">
                <div class="boxbotmimg">
                  <img class="img-responsive" src="./sites/all/themes/korhanifashion/images/AnchorLine_BLCR_HI.png">
                  <span class="hovblck"></span>
                </div>
                <div class="botboxhov">
                  <a href="https://www.facebook.com/KORHANIhome">
                    <img class="bbres" src="./sites/all/themes/korhanifashion/images/fusonfbok.png"></a>
                </div>
              </div>
            </div>

I want to manage these Images from back-end i.e from Admin-panel. So that Admin can change the images from Admin-panel, And also how Admin can put links to that images from back-end. How I can do that? Thanks in Advance


Solution

  • I usually solve similar issues(customizing the dynamic home page contents) by creating a custom content type(https://www.drupal.org/node/306792).

    And then you can hard-code to read nodes of a specific type in your template file or use Views (https://www.drupal.org/project/views).

    Added sample code:

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'home_image')
          ->entityCondition('status', 1);
    $result = $query->execute();
    
    if (isset($result['node'])) {
        $nodes = node_load_multiple(array_keys($result['node']));
        foreach ($nodes as $node) {
            $img_url = file_create_url($node->field_image['und'][0]['uri']);
        }
    }
    

    In the above code, 'home_image' is the name of content type you have created and 'field_image' is the name of image field. You can add and edit the name of image field when editing fields of content type. Please be sure to set the field type to 'Image'.