What rule should I follow to create a block (for block name,type etc)? - magento

I'm confused while creating a block or call a block. in phtml file suppose in footer.phtml file if I want to call a static block then I write
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_name')->toHtml(); ?>
and for newsletter(which is in template folder in my theme folder)
<?php echo $this->getLayout()->createBlock('newsletter/subscribe')->setTemplate('newsletter/subscribe.phtml')->toHtml(); ?>
so what should I write in footer.phtml to call a built in block(which is in base folder) like calendar,captcha etc ?
please tell me how can I call those in phtml file and xml file.
please tell me
1. <?php echo $this->getLayout()->createBlock('**?**')->setTemplate('**?**')->toHtml(); ?>
2. xml block code and where to put the code
3. rules to write block type and name.
-Thanks.

The only difference between the two blocks that you are mentioning is the type. The cms/block type is a built in way for you to create arbitrary text blocks with optional references to additional content (additional information native to Magento via widgets such as links or calls to other blocks).
The second block that you list is that of a block that represents a specific block which is used to output a specific model with a specific template. If you dive into the structure of Magento, you will find that the code that is core to Magento exists in the app/code/core/Mage folder. Inside of that, you will find items such as catalog/category, catalog/product, newsletter/subscribe etc. Additionally, according to MVC, you will need a way to present that model to the user via a view, or template by Magento's terms. Views into models will exist in the app/design/frontend/{package}/{theme}/template/ folder. You should find some continuity between the two sets of folders and will arrive to a set of views that you can use to output a block. In this case of a product, you will find app/code/core/Mage/catalog/product/ and app/design/frontend/base/default/template/catalog/product/view.phtml.
Hopefully this will set you on your way to better understanding the beast that is Magento. Magento, as described by Alan Storm, is not your father's PHP.

Related

Theme is caching previous user name

We are using CAS to login to our Drupal instance. This is working correctly and displaying the correct user content (blocks etc. based on roles). What is not working correctly is the small snippet in the theme that says welcome . It keeps showing the previous user who logged in.
How do I set this in bigpipe?
The code looks like this in the theme: <span id="user_name">{{user.displayname}}</span>
Is there a way to tell bigpipe not to cache this?
This code snippet is on one of our twig files header.twig.html which is a partial.
I ended up putting this in a block, and just referencing the block section in the theme instead of just pulling that, then I used the block to be ignored for caching.
Thanks!
I used this post with other resources to solve a similar problem. We were including {{ user.displayname }} in a twig template for the header on all pages of our site. Some users were seeing other users' names in the header after signing in. We wanted to solve the problem while impacting caching as little as possible. Here, I share in detail what we did in the hope that it will help others. I'll use the specific names used in our code. Readers will need to adjust to their own names. (The code follows our prescribed format, so please forgive that it isn't standard.)
Step 1
Create a custom module. Custom module creation is covered adequately in other places, so I won't give details here. Our custom module is named rsc.
Step 2
Create the folder modules/custom/rsc/src/Plugin/Block and in it create a file named DisplayName.php.
Step 3
In the file DisplayName.php, include the following:
<?php
namespace Drupal\rsc\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\user\Entity\User;
/**
* A block to show the user's display name
*
* #Block(
* id = "display_name",
* admin_label = "Display Name"
* )
*/
class DisplayName extends BlockBase // The class name must match the filename
{
public function build()
{
$user = User::load(\Drupal::currentUser()->id());
return
[
'#type' => 'markup',
'#markup' => $user->getDisplayName(),
'#cache' => ['contexts' => ['user']]
];
}
}
Step 4
Clear the Drupal cache to make the new block available at Block Layout.
Step 5
Go to Admin > Structure > Block Layout and place the Display Name block in the Hidden Blocks For Referencing - Not Displayed section (bottom of the list on our site). In the Configure Block dialog, edit the Machine name to display_name to match id in the code above. Clear the Display title checkbox and save. Doing this makes the block available for use in twig templates.
Step 6
In the twig template for the header, replace
{{ user.displayname }}
with
{{ drupal_entity('block', 'display_name') }}
The drupal_entity function is part of the twig_tweak module, which we were already using. It allows insertion of a custom block into a twig template. If you're not using this module, you'll need to install it, or find another method of including a block in a twig template.
Step 7
Clear the Drupal cache to make the modified template take effect.
If you see anything about this that can be improved, please comment.

Smarty getting page content

I need to edit a page on prestashop, I've found that code
<ul id="idTab2" class="bullet">{$agencies->content}</ul>
And where should i search for that $agencies variable ?
I have found the text which is being displayed into that place in CMS.
However I'd that variable need to be define somewhere, am I right ? Anyone knows where should i search for that ? I'm new to prestashop.
Why am i asking for this ? I need to add another page for example
<ul id="idTab2" class="bullet">{$test->content}</ul>
- but I can't just simply add another page called test.
The {$agencies} variable is being set in a object derived from either the Controller or Module classes but to be honest it looks like you're working with code that has been customised (via a class override or a module) making it impossible to provide a definitive answer to your question without knowing more detail.
If you can locate the term 'agencies' in a file located under \controllers, \modules or \override in your installation, then you will be closer to finding your answer. It will be contained in a function call similar to:
$this->context->smarty->assign('agencies' , [some-variable]);
Note that the parameters to the function may also be passed as an array for multiple assignments.

Examining Magento's final XML structure

Anyway to examine the final XML structure magento comes up with after parsing & combining all the different XML files?
There is nothing of that sort which turned up on searching on the internet and I think for someone like me, magento layouts were a bit too much in the beginning & I would try to do everything on the code side.
Another thing which will help in picking up the name of different nodes that we can use, right away from the final XML structure.
Never ran into this but I believe we will have a better picture of what's overriding what.
The following will get you the merged configuration from app/etc/*.xml, app/etc/modules/*.xml, as well as each (active) module's config.xml file; when retrieving the config though there is no indication of what was overwritten, as the merges happen as each config file is being parsed:
Mage::getConfig()->getNode()->asNiceXml(); // or asXML() | asArray() | etc.
However, you seem to be asking about how the application makes use of this information. This is a function of application design.
Also, you mention "all of the different XML files." It's worth noting that these are not maintained in one massive object instance. For example, layout XML is accessed using the layout update object Mage_Core_Model_Layout_Update and can be accessed meaningfully after it's been loaded and manipulated for a given rendering scope (e.g. loadLayout() in a controller action):
Mage::app()->getLayout()->getUpdate()->asString(); // or asSimplexml() or asArray()
Yes - Commercebug. As well as a whole load of other useful features, you can also view the entire XML structure that Magento has produced.
http://store.pulsestorm.net/products/commerce-bug-2
I believe the following will output the XML: echo Mage::getConfig()->getXmlString();
You can create a script with something like this:
header("Content-Type:text/xml");
require_once '../app/Mage.php';
Mage::app();
echo Mage::getConfig()->getXmlString();
based on answer from benmarks I did
echo "<pre>".htmlspecialchars(Mage::getConfig()->getNode()->asNiceXml())."</pre>";
If you want for example to see the blocks configuration in Magento 1 you can put this in a file, place the file at the root of the site and navigate to it in a browser:
<?php
include("app/Mage.php");
Mage::app();
//just see blocks...
echo "<pre>".htmlspecialchars(Mage::getConfig()->getNode()->global->blocks->asNiceXml())."</pre>";
die();

Magento - Make a copy of history.phtml and use it in my own way

I’m looking to find a way to copy the page history that we can access from My account/My orders which is available on this directory template/sales/order/history.phtml and use its content on my own way without affecting the original one. I’ve been trying many ways, as copying the whole directory and editing the Xml files related to it in order to setup up the right path and make it work, unfortunately it was a failure. I would like to know if you could give me a solution for this.
thx.
To use the functions of a block inside another .phtml I'm quite sure you can use getBlock
$blockFunctions = $this->getLayout()->getBlock('sales/order_history');
$order = $blockFunctions->getOrderHistory();
And to add a block in your custom module you'll need to create a .xml file for your block and add it to your template, you'll also have to add the actual .phtml file. Take a look at the moduleCreator (http://www.magentocommerce.com/magento-connect/danieln/extension/1108/modulecreator) this handles most of this quite well.
This is by no means througher its just a rough guide.

Magento, translate validation error messages

I have successfully created new rules for the prototype validation, now I need to translate the error messages (Location: String in Javascript). However, I can only translate all the messages, my new custom ones don't appear to be translatable. How do I change this?
Maybe you need an jstranslator.xml file inside etc folder:
<?xml version="1.0" encoding="UTF-8"?>
<jstranslator>
<some-message-name translate="message" module="mymodule">
<message>This is the text in my own js validation</message>
</some-message-name>
</jstranslator>
With the following structure and meanings:
<jstranslator> - [one] XML root node.
<some-message-name> - [zero or more] root node child elements with a unique XML element name across all jstranslator.xml files (otherwise last in loading order based on module listing wins).
Attributes:
translate="message" - (optional) a hint that the child element(s) that is being translated is named "message", however this is hardencoded for the js translation XML files (Magento CE 1.9, search for "*/message") and this attribute does not need to be used.
module="mymodule" - (optional) name of the module, if left out the value is "core". It will be used to instantiate the data-helper later on (from that module) which then is reponsible to load the translations (e.g. from the CSV files).
<message> - [zero or one per parent] message to translate. the text value of this elements node-value is taken to be added to the javascript Translator object data.
All jstranslator.xml files of activated modules are processed.
Then put your translation line into the Something_Mymodule.csv file:
"This is the text in my own js validation", "(translated in a different language or speech)"
Then in your js scripts you can use your own translations via the Translator:
Translator.translate('This is the text in my own js validation');
Further References
Correct usage of jstranslator.xml
To translate custom javascript error messages you need also to add them to the following file:
\app\code\core\Mage\Core\Helper\Js.php
find a function _getTranslateData()
and you'll see a bunch of messages already in there.
just add your message somewhere in the array like this:
'This is my validation message' => $this->__('This is my validation message')
Don't forget a comma (,).
And then put translation in some translate file.
In the file where you use this message (I use it in opcheckout.js file) you need to wrap text in Translator.translate('This is my validation message').
I haven't figured out yet if it's important which translate file that is. You can try Mage_Core.csv .
I needed it in Mage_Checkout.csv and it works in there.
Anyway, for those who are interested in more, I noticed that these javascript messages are printed in header of every html page and some worrie that it messes with the SEO. Anyway this is printed in file
\app\design\frontend\bmled\default\template\page\html\head.phtml with the code.
<?php echo $this->helper('core/js')->getTranslatorScript() ?>
Check for more here:
I hope this helps, I just hope this works everywhere, so far I tested it on Onepage Checkout only.
You can edit app/local/ur_language/Mage_Core.csv file, adding original string in the first Column the translated one in the second. All the javascript translations are stored in this file.
What's helped me (Magento EE 1.6) -
I added new translation object:
<script>
var _data = {
'This is a required field.':
"<?php echo $this->__('This is a required field.'); ?>",
'Please make sure your passwords match.':
"<?php echo $this->__('Please make sure your passwords match');?>",
'validate telephone error':
"<?php echo $this->__('validate telephone error');?>"
};
var Translator = new Translate(_data);
</script>
if it is defined VarienForm uses it in js validation
We had exactly the same problem with one of our magento projects. We found that the function in app/design/frontend/default/default/template/page/htmlhead.phtml had been commented out:
<?php echo $this->helper('core/js')->getTranslatorScript() ?>
After putting this in, it still did not work, because translations had not been inserted into translate file. Check out those two things and it should start working.
To expand on this, you must add the translation strings to Mage/Core/Helper/Js.php.

Resources