Drupal facet change layout - php

Is there any way to change the layout of a facet?
I know you can create a file in the template dir named: block-apachesolr_search-[field].tpl.php
The problem I am having is that at this stage the html in the block variable has already been created.
Is there any way to change the html or just get the elements of the facet?
Thanks!!

are you talking about faceted search module?
you could overwrite stuff by adding it in your template file or something simmilar, forinstance i had to add the "clear-block" to a div and did it this way:
function themename_faceted_search_ui_facet_wrapper($env, $facet, $context, $content) {
$classes = array(
'faceted-search-facet', // Note: Tooltips rely on this class.
'faceted-search-env-'. $env->name,
'faceted-search-facet--'. check_plain($facet->get_key() .'--'. $facet->get_id()), // Note: Tooltips rely on this class.
'faceted-search-facet-'. ($facet->is_active() && $context != 'related' ? 'active' : 'inactive'),
'faceted-search-'. $context,
);
return ''. $content .''."\n";
}
took a while to find the right function in the module file, but i dont know of any other way.

Related

TYPO3: Add LINK tags to CSS Files in the HEAD tag from within a Typo 3 extension

I have a custom extension in Typo3.
Does anyone know how to add in a template ?
Ive had some success with adding in
call_user_func(
function($extKey)
{
//other init code goes here
$GLOBALS['TBE_STYLES']['skins'][$extKey] = array();
$GLOBALS['TBE_STYLES']['skins'][$extKey]['name'] = $extKey;
$GLOBALS['TBE_STYLES']['skins'][$extKey]['stylesheetDirectories'] = array(
'structure' => 'EXT:'. $extKey. '/Resources/Public/css/',
);
},
$_EXTKEY
);
Ive looked at typo3 docs but cant get it to work when adding to ext_tables.php
It is a Front End Plug in and I want the reference to be contained within the Extension for ease of installation and management.
Anyone had success with this ?
put following in this file your_extension/Configuration/TypoScript/setup.ts
page = PAGE
page {
# CSS files to be included
includeCSS {
yourCss = EXT:your_extension/Resources/Public/Css/yourCss.css
}
}
and then call it in your ext_tables.php
/***************
* Include TypoScript
*/
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile(
$_EXTKEY, 'Configuration/TypoScript', 'Your Package'
);
you'll find this voice in your template in the backend so that you can include it ...

Can we edit a mustache template in runtime?

Can I add new nodes to mustache template at run-time in PHP? Let's say below is a code where ProductDetails will contain few single products:
{{#ProductDetails}}
{{#SingleProduct}}
{{OldDetail}}
{{/SingleProduct}}
{{/ProductDetails}}
I want to add a new node like {{NewDetail}} just after {{OldDetail}} through some function in run-time(i.e just before I am compiling the template as these templates have been shipped to customers in such a way that only code to compile can be changed but not the template)? I don't want to do string manipulation(customers created few new templates with above parameters present at least but the spacing may change & few new entries can be added by them around nodes). Does mustache library provide any functions for that?
If I were you, I will try Lambdas.
Template.mustache will be like this:
{{#ProductDetails}}
{{#SingleProduct}}
{{OldDetail}}
{{/SingleProduct}}
{{/ProductDetails}}
And codes will be like:
$Mustache = new Mustache_Engine(['loader' => new Mustache_Loader_FilesystemLoader($YOUR_TEMPLATE_FOLDER),]);
$Template = $Mustache->loadTemplate('Template');
$OriginalOldDetail = '<h1>this is old detail</h1>';
echo $Template->Render([
'ProductDetails' => true,
'SingleProduct' => true,
'OldDetail' => function($text, Mustache_LambdaHelper $helper){
// Render your original view first.
$result = $helper->render($text);
// Now you got your oldDetail, let's make your new detail.
#do somthing and get $NewDetail;
$NewDetail = $YourStuff;
// If your NewDetail is some mustache format content and need to be render first.
$result .= $help->render($NewDetail);
// If is some content which not need to be render ? just apend on it.
$result .= $NewDetail;
return $result;
}
]);
Hope that will help.
(English is not my first language so hope you can understand what I'm talking about.)

Trying to highlight current menu item in Drupal 8 using hook_preprocess_menu

I have the below function to create active trail functionality. So if I were to have /blog as a "parent" and a post of /blog/mypost, when on mypost the blog link would show as highlighted. I don't want to have to make menu items for all the blog posts. The problem is when caching is turned on (not using settings.local.php and debug turned off) the getRequestUri isn't changing on some pages. It seems to be cached depending on the page. It works fine with page caching turned off but I'd like to get this working with caching. Is there a better way to check for the current path and apply the active class?
function mytheme_preprocess_menu(&$variables, $hook) {
if($variables['theme_hook_original'] == 'menu__main'){
$node = \Drupal::routeMatch()->getParameter('node');
if($node){
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
// If current path starts with a part of another path i.e. a parent, set active to li.
if (0 === strpos($current_path, $item['url']->toString())) {
// Add active link.
$variables['items'][$key]['attributes']['class'] .= ' menu-item--active-trail';
}
}
}
}
}
I've also tried putting this into a module to try and see if I can get the current path to then do the twig logic in the menu--main.twig.html template but I have the same problem.
function highlight_menu_sections_template_preprocess_default_variables_alter(&$variables) {
$variables['current_path'] = $_SERVER['REQUEST_URI'];
}
After a very long time trying all sorts of things, I found an excellent module which addresses exactly this problem. Install and go, not configuration, it just works:
https://www.drupal.org/project/menu_trail_by_path
Stable versions for D7 and D8.
I tried declaring an active path as part of a custom menu block, and even then my declared trail gets cached. Assuming it's related to the "There is no way to set the active link - override the service if you need more control." statement in this changelog, though why MenuTreeParameters->setActiveTrail() exists is anybody's guess.
For the curious (and for me when I search for this later!), here's my block's build() function:
public function build() {
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$parameters->setRoot('menu_link_content:700c69e6-785b-4db7-be49-73188b47b5a3')->setMinDepth(1)->setMaxDepth(1)->onlyEnabledLinks();
// An array of routes and menu_link_content ids to set as active
$define_active_mlid = array(
'view.press_releases.page_1' => 385
);
$route_name = \Drupal::request()->get(RouteObjectInterface::ROUTE_NAME);
if (array_key_exists($route_name, $define_active_mlid)) {
$menu_link = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(array('id' => $define_active_mlid[$route_name]));
$link = array_shift($menu_link);
$parameters->setActiveTrail(array('menu_link_content:' . $link->uuid()));
}
$footer_tree = $menu_tree->load('footer', $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($footer_tree, $manipulators);
$menu = $menu_tree->build($tree);
return array(
'menu' => $menu,
);
}
[adding a new answer since this is a completely different approach than my earlier one]
If a CSS-based solution is acceptable, this seems to work okay:
.page-node-type-press-release {
a[data-drupal-link-system-path="press-room/press-releases"] {
// active CSS styles here
}
}

Drupal 7 Render a Comment Object

So this is the problem I am running into. If I have a comment object, I want to create a renderable array that is using the display settings of that comment. As of now this is what I have:
$commentNew = comment_load($var);
$reply[] = field_view_value('comment', $commentNew, 'comment_body', $commentNew->comment_body['und'][0]);
Which works fine because I dont have any specific settings setup for the body. But I also have image fields and video embed fields that I need to have rendered the way they are setup in the system. How would I go about doing that?
Drupal core does it with the comment_view() function:
$comment = comment_load($var);
$node = node_load($comment->nid);
$view_mode = 'full'; // Or whatever view mode is appropriate
$build = comment_view($comment, $node, $view_mode);
If you need to change a particular field from the default, use hook_comment_view():
function MYMODULE_comment_view($comment, $view_mode, $langcode) {
$comment->content['body'] = array('#markup' => 'something');
}
or just edit the $build array received from comment_view() as you need to if implementing the hook won't work for your use case.

Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled

I have tried the following two methods in catalog/navigation/vert_nav.phtml to add or suppress content specific to the home page:
if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))):
or
if(
Mage::getSingleton('cms/page')->getIdentifier() == 'home' &&
Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'
) :
Both work fine, however when BLOCK_HTML cache is turned on, it works at first, then after a while the home page starts displaying content that is intended only for other pages (after an else clause I use lower down). When I turn off the BLOCK_HTML, it behaves as expected.
Interestingly I've used the same code (the 1st one) in page/html/head.phtml (for home page specific javascript/css), and in page/html/header.phtml (for a header banner that should only appear on the home page), and these work fine even when BLOCK_HTML is ON.
(Magento 1.4.1.1)
The above answer is the best solution.
You could simply copy app/code/core/Mage/Catalog/Block/Nagivation.php
to:
app/code/local/Mage/Catalog/Block/Nagivation.php
and then change the getCacheKeyInfo() method as described above.
/**
* Get Key pieces for caching block content
*
* #return array
*/
public function getCacheKeyInfo()
{
$shortCacheId = array(
'CATALOG_NAVIGATION',
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
'template' => $this->getTemplate(),
'name' => $this->getNameInLayout(),
$this->getCurrenCategoryKey(),
// Your logic to make home/none home have different cache keys
Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1' : '0'
);
$cacheId = $shortCacheId;
$shortCacheId = array_values($shortCacheId);
$shortCacheId = implode('|', $shortCacheId);
$shortCacheId = md5($shortCacheId);
$cacheId['category_path'] = $this->getCurrenCategoryKey();
$cacheId['short_cache_id'] = $shortCacheId;
return $cacheId;
}
This will make the cache key different for homepage / none-homepage pages, which will cache two copies, rather than caching a single template copy for use on all pages.
Here are sources you'd want to read about Block Html cache:
magento forum
some blog
inchoo blog
It would be better for performance to not disable the block completely, but rather specify the cache key in a smart way. So here's what you should do:
First - specify a custom block for your .phtml file. If you don't know what Block is, or how to assign a block to a template file, here's the reference to Alan Storm blog.
Second - you will have to add next code to a Block constructor:
$this->addData(array(
'cache_lifetime' => 3600,
'cache_tags' => array(Mage_Cms_Model_Block::CACHE_TAG),
'cache_key' => $this->getCacheKey(),
));
As you see, I used here the getCacheKey method from the abstract class Mage_Core_Block_Abstract.
Now you need to make sure the cache_key works for your logic. The Mage_Core_Block_Abstract::getCacheKey uses other method, which should actually specify the unique values for our block - getCacheKeyInfo. You need to redefine it using your logic:
public function getCacheKeyInfo()
{
$isHomepage = 0;
if (Mage::getSingleton('cms/page')->getIdentifier() == 'home') {
$isHomepage = 1;
}
return array(
$this->getNameInLayout(),
$isHomepage,
);
}
Now you can be sure that cache key for Home Page will differ from cache key to all other your pages, and your cache will return valid info.
Just to add to these answers suggesting to check if current page identifier equals to "home".
It would be definitely safer to compare it with Mage::getStoreConfig('web/default/cms_home_page') instead.
We use
<!-- SNH CUSTOM -->
$route = Mage::app()->getFrontController()->getRequest()->getRouteName();
$action = Mage::app()->getFrontController()->getRequest()->getActionName();
if($route == 'cms' && $action == 'index'):
<div class="grid_12">
echo $this->getChildHtml('shopper_footer_partners');
</div>
endif;
really the best way is to:
1 Update your layout XML (local.xml or theme custom.xml)
<!-- CUSTOM: ADD NEW FOOTER BLOCK AT BOTTOM FOR PARTNERS -->
<cms_index_index>
<reference name="footer">
<block type="cms/block" name="footer_block_extra">
<action method="setBlockId"><block_id>footer_block_extra</block_id></action>
</block>
</reference>
</cms_index_index>
and step 2 add this code where you want the block in your template phtml (often /page/html/footer.phtml)
<!-- SNH CUSTOM -->
<div class="grid_12">
<?php echo $this->getBlockHtml('footer_block_extra'); ?>
</div>
and step 3 create a new CMS block in your backend with the ID "footer_block_extra" ... and add your content.

Categories