Search code examples
regexxmlopencartvqmod

Regex to add <li> tag using OCMOD


I want to add a menu in opencart admin page using OCMOD. I want to modify the menu in menu.tpl file but I think it requires regex as the <li> must be the last before </ul> tag. Can somebody help? The <ul> tag is like this:

    <ul>
      <li><a href="<?php echo $location; ?>"><?php echo $text_location; ?></a></li>
      <li><a href="<?php echo $language; ?>"><?php echo $text_language; ?></a></li>
      <li><a href="<?php echo $currency; ?>"><?php echo $text_currency; ?></a></li>
      <li><a href="<?php echo $stock_status; ?>"><?php echo $text_stock_status; ?></a></li>
      <li><a href="<?php echo $order_status; ?>"><?php echo $text_order_status; ?></a></li>
      <li><a class="parent"><?php echo $text_return; ?></a>
        <ul>
          <li><a href="<?php echo $return_status; ?>"><?php echo $text_return_status; ?></a></li>
          <li><a href="<?php echo $return_action; ?>"><?php echo $text_return_action; ?></a></li>
          <li><a href="<?php echo $return_reason; ?>"><?php echo $text_return_reason; ?></a></li>
        </ul>
      </li>
      <li><a href="<?php echo $country; ?>"><?php echo $text_country; ?></a></li>
      <li><a href="<?php echo $zone; ?>"><?php echo $text_zone; ?></a></li>
      <li><a href="<?php echo $geo_zone; ?>"><?php echo $text_geo_zone; ?></a></li>
      <li><a class="parent"><?php echo $text_tax; ?></a>
        <ul>
          <li><a href="<?php echo $tax_class; ?>"><?php echo $text_tax_class; ?></a></li>
          <li><a href="<?php echo $tax_rate; ?>"><?php echo $text_tax_rate; ?></a></li>
        </ul>
      </li>
    </ul>

I know it is posible to add my menu after

<li><a href="<?php echo $geo_zone; ?>"><?php echo $text_geo_zone; ?></a></li>

Using this operation

<file path="admin/view/template/common/menu.tpl">
    <operation>
        <search>
            <![CDATA[
            <li><a href="<?php echo $geo_zone; ?>"><?php echo $text_geo_zone; ?></a></li>
            ]]>
        </search>                       
        <add position="after">
            <![CDATA[
            <li><a href="<?php echo $my_menu; ?>"><?php echo $text_my_menu; ?></a></li>
             ]]>
        </add>
    </operation>
</file>

But I want to add my menu to the last of the list (after <li> that contains <ul>). I think it should use regex to make this works. Please somebody help, thank you.


Solution

  • The best way to do this is to use just a standard search and specify the </ul> you want to using the index in your <add> tag specifying which of them is the right one to place the content before/after. Remember that other extensions can do the same and add sub-menus that will alter the index. Regex isn't a viable option for this.

    So the operation for your question should be like this:

    <file path="admin/view/template/common/menu.tpl">
    <operation>
        <search index="2">
            <![CDATA[
            </ul>
            ]]>
        </search>                       
        <add position="before">
            <![CDATA[
            <li><a href="<?php echo $my_menu; ?>"><?php echo $text_my_menu; ?></a></li>
             ]]>
        </add>
    </operation>
    

    The index starts from 0, we add new list before the last </ul> which is in index 2.