Better way to overide content page in drupal - php

I am creating custom content pages using this function in my template file
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type)) {
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}
Then I am creating a custom page--"content_name".tpl.php file for my content. However this also overrides the edit, moderate, track pages for that content as well. I only want it to override the main content page. Is there an easy way to do this.

Change your code to be something like:
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type) && is_null(arg(2))) { // the edited line
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}

Related

Get instances to arbitrary plugins in php files

The name is quite bad, but I really don't know what else to call it.
I'm trying to make a extendable and modular plugin system for my website. I need to be able to access plugin php files that exist in a plugin directory and get access to their classes to call functions such as getting the html content that the plugin should show and more.
Below is a semi-pseudo code example of what I am trying to achieve, but how to actually arbitrarily load the plugins is where I am stuck (PluginLoader.php).
-Max
//BasePlugin.php
abstract class BasePlugin
{
public function displayContent()
{
print "<p>Base Plugin</p>";
}
};
//ExamplePlugin.php -> In specific plugin directory.
require('../BasePlugin.php');
class ExamplePlugin extends BasePlugin
{
public static function Instance()
{
static $inst = null;
if ($inst === null) {
$inst = new ExamplePlugin();
}
return $inst;
}
public function displayContent()
{
print "<p>Example Plugin</p>";
}
}
//PluginLoader.php
foreach($pluginFile : PluginFilesInDirectory) { // Iterate over plugin php files in plugin directory
$plugin = GetPlugin($pluginFile); // Somehow get instance of plugin.
echo plugin->displayContent();
}
I'm guessing here, but it seems to me that you need to:
get a list of the plugins in the desired directory.
include or require the plugin's class file.
create an instance of the class.
call the plugin's displayContent() method.
So, you probably want to do something like
$pluginDir = 'your/plugin/directory/' ;
$plugins = glob($pluginDir . '*.php') ;
foreach($plugins as $plugin) {
// include the plugin file
include_once($plugin) ;
// grab the class name from the plugin's file name
// this finds the last occurrence of a '/' and gets the file name without the .php
$className = substr($plugin,strrpos($plugin,'/') + 1, -4) ;
// create the instance and display your test
$aPlugin = $className::Instance() ;
$aPlugin->displayContent() ;
}
There's probably a cleaner way to do it, but that will ready your directory, get the plugins' code, and instantiate each one. How you manage/reference them afterwards depends on how your plugins register with your application.

Prestashop 1.6 custom module : addJS/addCSS don't work properly

I created my prestashop module, with a hook to display my specific search form.
public function hookDisplayTopColumn($params)
{
$this->context->controller->addCSS($this->_path.'css/modelfilter.css', 'all');
$this->context->controller->addJS($this->_path.'js/modelfilter.js');
$marque = $this->getSubCategories($this->marquesCategory);
$this->context->smarty->assign(array(
'marques' => $marque,
));
return $this->display(__FILE__, 'form_model.tpl');
}
JS and CSS files are not included. To find why, I added a parse line in classes/controller/FrontController.php :
public function addMedia($media_uri, $css_media_type = null, $offset = null, $remove = false, $check_path = true)
{
echo 'addMedia '.$media_uri."<br/>\n";
And the result is : all css/js files appears before the beginning of the page (just after <body>), but my files comes just before displaying form_model.tpl
Please, how to make my files to be called in the good time ?
You shall not use $this->context->controller->addCSS and addJS outside of hookDisplayHeader().
When the header of your page is built hookDisplayHeader() is called to add headers elements. Once this hook is done, the header is built and can't be changed.
So when the hook hookDisplayTopColumn() is called, the header is already built.
To add your files you have to implement the hookDisplayHeader() in your module:
public function hookDisplayHeader($params)
{
$this->context->controller->addCSS($this->_path.'css/modelfilter.css', 'all');
$this->context->controller->addJS($this->_path.'js/modelfilter.js');
}
And remove those lines from hookDisplayTopColumn().

Open Graph Meta Tag Overwritting Yoast, WordPress

I'm trying to change the og:url content on specific posts but I am unsure how to implement my changes from the functions.php file.
I have tried doing this using the content I have found on the internet but believe it has been updated since then.
I have updated the class-opengraph.php file in the the wordress-seo plugin folder which works, please find my edits below:
public function url() {
$url = apply_filters('wpseo_opengraph_url',
WPSEO_Frontend::get_instance()->canonical(false));
if (is_string($url) && $url !== '' ) {
if (is_page(32721)) {
$this->og_tag('og:url', esc_url('testing'));
} else {
$this->og_tag( 'og:url', esc_url( $url ) );
}
return true;
}
return false;
}
It's not good to modify the plugin files directly because when you update the plugin, you will lose all your changes to those files.
There are two solutions that I have found to do such thing.
You get the instance of the class WPSEO_Frontend, then update it's options for the og:url.
e.g.
$object = WPSEO_Frontend::get_instance();
$object->options['og_url'] = esc_url( $url );
This can be added before the wp_head()
You can use add_filter to hook a function to the filter action. We use the filter action below.
Filter: 'wpseo_opengraph_url' - Allow changing the OpenGraph URL
e.g.
function update_og_url($url) {
return "http://www.yoursampleurl.com";
}
add_filter('wpseo_opengraph_url', 'update_og_url', 10, 1);
Source: Wordpress SEO API

Magento - Include TPL files in CMS

I'm performing a quick job for a Magento-based site and can't recall how to pull in TPL files within the CMS. I've tried using the following code in my CMS page...
{{block type="cms/block" block_id="page_heading" template="cms/content_heading2.phtml"}}
The TPL file is already in the correct folder... app/design/frontend/default/wfs/cms
I'm just not sure how to include this PHTML file correctly. Is it possible to provide the correct syntax?
Thanks!
When you say
`type="cms/block"`
you're telling Magento to create a 'cms/blocktemplate object, which translates to aMage_Cms_Block_Block` class. If you take a look at this block's source
#File: app/code/core/Mage/Cms/Block/Block.php
protected function _toHtml()
{
$blockId = $this->getBlockId();
$html = '';
if ($blockId) {
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load($blockId);
if ($block->getIsActive()) {
/* #var $helper Mage_Cms_Helper_Data */
$helper = Mage::helper('cms');
$processor = $helper->getBlockTemplateProcessor();
$html = $processor->filter($block->getContent());
}
}
return $html;
}
you can see this doesn't render templates, but instead renders Magento's static block objects.
Try
`type="core/template"`
instead, and make sure your block ID is unique.

render html data stored in variable in function of .module file in page.tpl.php file

I have just started using drupal and am little bit confused with the flow. I have a variable $var = "<div>Render in View</div>" in a function in my .module file. How do I render this exact html in the page.tpl.php file?
Thanks
This might work: http://api.drupal.org/api/drupal/modules%21system%21theme.api.php/function/hook_preprocess/7
function mymodule_preprocess(&$variables, $hook) {
if ($hook == 'page') {
$variables['my_custom_page_var'] = '<div>Render in page.tpl.php</div>';
}
}
or use the dynamic hook name
function mymodule_preprocess_page(&$variables) {
$variables['my_custom_page_var'] = '<div>Render in page.tpl.php</div>';
}
Then this should be accessible in page.tpl.php as $my_custom_page_var.

Categories