Powered By Blogger

Thursday, 12 September 2013

Custom module layout file seems to be ignored

I have a couple of blocks that I’d like to include on multiple sites, so I’ve built a simple module to contain them. The problem I’m having is that the custom layout XML file that I’ve defined in the module’s config file appears to be ignored. If I place the XML from my custom layout in one of the existing XML layout files, I see my blocks. So I know that the module is generally working (at least that my blocks work correctly). But if I remove the block from an existing layout XML file and only have it in the my custom layout file, the block no longer appears.

Here is the relevant portion of my module’s config file (app/code/local/Abc/Affiliate/etc/config.xml): 



<?xml version="1.0"?><config>
    <modules>
        <Abc_Affiliate>
            <version>0.1.0</version>
        </Abc_Affiliate>
    </modules>
    <frontend>
        <routers>
            <affiliate>
                <use>standard</use>
                <args>
                    <module>Abc_Affiliate</module>
                    <frontName>affiliate</frontName>
                </args>
            </affiliate>
        </routers>
        <layout>
            <updates>
                <affiliate>
                    <file>affiliate.xml</file>
                </affiliate>
            </updates>
        </layout>
    </frontend>

    <adminhtml>
        <layout>
            <updates>
                <affiliate>
                    <file>affiliate.xml</file>
                </affiliate>
            </updates>
        </layout>
    </adminhtml>
</config>



That defines a custom layout file “affiliate.xml”. Here are the contents of that file (app/design/frontend/default/default/layout/affiliate.xml):




<?xml version="1.0"?><layout version="0.1.0">
    <default>
        <!-- Abc_Affiliate -->
        <reference name="before_body_end">
            <block type="affiliate/tracker" name="affiliate_tracker" as="affiliate_tracker" />
        </reference>
    </default>
</layout>



With the configuration set this way, I don’t see the “affiliate_tracker” block.
I’ve tried moving this portion to app/design/frontend/default/default/layout/page.xml, and then the block appears:



<reference name="before_body_end">
     <block type="affiliate/tracker" name="affiliate_tracker" as="affiliate_tracker" />
</reference>



But I don’t want to modify page.xml on multiple sites, and would like to have this layout update info in my module.

Any advice about what to change to get my module’s layout file to be recognized? Any debugging advice that may help me figure out what layout files are loaded at any particular time? 

Try adding the following file to app/etc/modules
Abc_Affiliate.xml

<config>
   <modules>
      <Abc_Affiliate>
         <active>true</active>
         <codepool>local</codepool>
      </Abc_Affiliate>
</config>


enjoy ........ :)

PHP - AJAX and MySQL



<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>  



 PHP FILE 

The page on the server called by the JavaScript above is a PHP file called "getuser.php".

The source code in "getuser.php" runs a query against a MySQL database, and returns the result in an HTML table:



<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>  


........ :)

jQuery Ajax Function  


<!DOCTYPE html>
<html>
<title>jQuery Function Demo - jQuery4u.com</title>
<head>
    <script type="text/javascript">
    var JQFUNCS =
    {
        runFunc:
        {
            /* ------------------------------ ajax Demo ------------------------------ */
            "ajax":
            {
                run: function(id)
                {
                    $('#'+id+' .content1').bind('click', function(e)
                    {
                        e.preventDefault();
                        getContent('/function-demos/functions/ajax/data/content1.html');
                    });
                    $('#'+id+' .content2').bind('click', function(e)
                    {
                        e.preventDefault();
                        getContent('/function-demos/functions/ajax/data/content2.html');
                    });
                    $('#'+id+' .content3').bind('click', function(e)
                    {
                        e.preventDefault();
                        getContent('/function-demos/functions/ajax/data/content3.html');
                    });
                    function getContent(filename)
                    {
                        $.ajax({
                            url: filename,
                            type: 'GET',
                            dataType: 'html',
                            beforeSend: function() {
                                $('#'+id+' .contentarea').html('<img src="/function-demos/functions/ajax/images/loading.gif" />');
                            },
                            success: function(data, textStatus, xhr) {
                                if (filename == '/function-demos/functions/ajax/data/content3.html')
                                {
                                    setTimeout( function() {
                                        $('#'+id+' .contentarea').html(data);
                                    }, 2000);
                                }
                                else
                                {
                                    $('#'+id+' .contentarea').html(data);
                                }
                            },
                            error: function(xhr, textStatus, errorThrown) {
                                $('#'+id+' .contentarea').html(textStatus);
                            }
                        });
                    }
                },
                reset: function(id)
                {
                    $('#'+id+' .contentarea').html('Content will appear here.');
                    $('#'+id).hide();
                }
            }
        }
    }
    </script>
</head>
<body>
<h3>Demo: Using $.ajax() to load content without reloading the entire page.</h3>
<p>HTML content is held in separate files which are inserted below using AJAX. Note that it is also courtesy to show a loading image while the AJAX request is processing. Content 3 has a 2 second delay to simulate the loading image.</p>
<p><a href="#" id="ajax-demobtn" class="demobtn btn actionr">Run Demo</a> <a href="#" id="ajax-codebtn" class="codebtn btn actionr">View Code</a> <a href="#" id="ajax-resetbtn" class="resetbtn btn actionr" style="display: none;">Reset</a></p>
<div class="demoarea" id="ajax">
<div class="contentarea" style="border:1px solid orange;padding:10px;margin-bottom:5px;">Content will appear here.</div>
<button class="content1 btn">Load content 1</button> <button class="content2 btn">Load content 2</button> <button class="content3 btn">Load content 3</button></div>
</body>
</html>


Enjoy ........ :)