How to fetch block's label in xml from phtml in Magento? - magento

I have a custom block in my layout file like this:
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<label>Foo</label>
</block>
How do I get the value of label from bar.phtml ?
Please note I do not want to use setData function to set my variable and pass it.
I want to extract the value inside tags from the phtml (or anywhere else). I hope its clear.

I don't think there is a really classy Magento way of doing it since the label of a block purpose is not to be displayed as far as we speak of the frontend.
label: This element is introduced since Magento 1.4. It defines the label of the handle which is shown as a descriptive reference in some areas of the admin panel.
Source
I would really warmly recommend you to stay away from the code below. But if that is really what you want to achieve, this is a way :
First we get the layout = a big xml concatenation of the layout for that page containing the xml where the block is defined, and thus our label
$layout = $this->getLayout();
Then we get the current block name in the layout
$currentBlockNameInLayout = $this->getNameInLayout();
We can, then get the XML node representing the current block in the template.
getXpath() does returns an array, so that is why I used list() to get the first item off of this array
list($currentBlockInLayout) = $layout->getXpath("//block[#name='".$currentBlockNameInLayout."']");
We have what we want and can echo its label element
echo $currentBlockInLayout->label;
Take care though, this is an object of type Mage_Core_Model_Layout_Element so if you want to do anything else than displaying it, you would have to use the __toString() method
var_dump( $currentBlockInLayout->label->__toString() );
Full code :
$layout = $this->getLayout();
$currentBlockNameInLayout = $this->getNameInLayout();
list($currentBlockInLayout) = $layout->getXpath("//block[#name='".$currentBlockNameInLayout."']");
echo $currentBlockInLayout->label;
var_dump( $currentBlockInLayout->label->__toString() );

In your XML, use the action method setData
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<action method="setData">
<label>Foo</label>
</action>
</block>
Then in your bar.phtml file, you can retrieve it using $this->getData('label'):
<?php echo $this->getData('label') ?>

Related

How to pass .phtml file variable to another module .phtml file in magento2

I am new in the magento. If anyone have idea how to do this then please let me know.
I have two files in different module in in that two .phtml file will be there.
From First .phtml file to another .phtml file i want pass array variable
I am not getting how to pass that.
First file path as follows with php variable:
/var/www/html/MyProject/app/design/frontend/Megnor/mag110246_4/Lof_CustomerMembership/templates/customer/membership/transactions.phtml
In this file i have $transaction variable that i want to send another info.phtml
<?php
/** #var \Magento\Customer\Block\Account\Dashboard\Address $block */
$helper = $this->helper("Lof\CustomerMembership\Helper\Data");
$transactions = $block->getTransactions();
$address=$block->getPrimaryBillingAddress();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerId =$customerSession->getCustomer()->getId();
$activeOrNot="";
?>
Another file path will be:
/var/www/html/MyProject/app/design/frontend/Megnor/mag110246_4/Magedelight_SMSProfile/templates/account/dashboard/info.php
In the info.php file i want that $transactions array variable
Anyone have idea how to do this then please let me know
Your getTransactions() function inside in your block. so just reuse the block for your another phtml file.
Three options,
just copy your function from transactions block to info block, all related variables and constructions too.
From info block, create object from transactions block and this object to create getTransactions() function
Simply call it directly using
$blockObj= $block->getLayout()->createBlock('Company\ModuleName\Block\ClassName');
echo $blockObj->getTransactions();

Find dynamically created controls inside Literal in asp.net page

Following is my code
-- the .aspx file
<div id="testDiv" runat="server"></div>
-- the .aspx.cs file
Literal lit1 = new Literal();
lit1.Text = "<table class='allocTable'>";
lit1.Text += "<tr><td><input type='text' runat='server' id='testbox1'></input></td></tr>";
... some other controls <tr><td>...
lit1.Text += "</table>"
testDiv.Controls.Add(lit1);
Now how can I find testbox1 in the .aspx.cs file? I have used FindControl on testDiv and the placeHolder but it returns null.
It seems the textboxes have not been added to the page control but they have just been rendered.
If you want to get the value of the textbox on post back, you could use Request.Form["testbox1"].
If you want wo work with "real" server controls, you would use an asp:Panel in your aspx file instead of your div.
In the asp.cs Init or Load method you would then add an HtmlTable to the Panel.Controls collection, HtmlTableRow(s) to the HtmlTable.Rows collection and HtmlTableCell(s) to the HtmlTableRow.Cells collection.
As you create the input controls you are adding to the Cell.Controls collection, you can store them in private member variables.

Programmatically remove block from layout

I want to remove the product_options_wrapper block from the product view page according to the logedin user via frontend router controller.
I know that I can programmatically append a new block but I didn't find a remove function. :-(
Tried sth. like that
$this->getLayout()->unsetBlock('product_options_wrapper');
$this->getLayout()->getBlock('product.info')->remove('product_options_wrapper');
But nothing works.
Inorder to remove a block using its parent block use the code below
$this->getLayout()->getBlock('product.info')->unsetChild('product_options_wrapper');
The OP code should work, if it used the correct block name, which is product.info.options.wrapper, as opposed to the block alias.
$this->loadLayout();
//e.g.
if (Mage::getSingleton('customer/session')->getCustomerGroupId() == [id]){
$this->getLayout()->unsetBlock('product.info.options.wrapper');
}
$this->renderLayout();
This should work:
$blockName = 'left'; // Add yours
$update = Mage::app()->getLayout()->getUpdate();
$removeInstruction = "<remove name=\"$blockName\"/>";
$update->addUpdate($removeInstruction);
Why? Have a look in the file Mage_Core_Model_Layout in the method generateXml() the XML is parsed and where a remove is set for a block, the attribute ignore is added to the block. In the method generateBlocks() all the blocks which have that attribute are not added.

Output of getPaymentHtml() in Magento

I haven't been able to find where does the output of getPaymentHtml() comes from.
Its defined as:
public function getPaymentHtml() {
return $this->getChildHtml('payment_info');
}
I couldn't find out the template for payment_info block.
Basically I want to be able to retrieve credit card type and credit card number in the progress block of checkout.
How do I find out the method names? Something like $this->getCreditCardType()
Edit: OK! I understand that Magento figures out the payment method first which has their corresponding templates which are used to render output. But in progress.phtml of checkout, var_dump( $this instanceof Mage_Payment_Block_Info_Cc ); returns false, so how do I access that in current context?
The Progress block doen't have it's own template for Payment info. Mage_Checkout_Block_Onepage_Payment_Info block uses the selected Payment Method block to output html. Look at the Mage_Checkout_Block_Onepage_Payment_Info::_toHtml() method:
protected function _toHtml()
{
$html = '';
if ($block = $this->getChild($this->_getInfoBlockName())) {
$html = $block->toHtml();
}
return $html;
}
To find the actual template and block for the specific Payment method you use, you need to perform next steps:
First - get model alias for current payment method Mage::getStoreConfig('payment/'.$yourMethod.'/model') and instantiate it using Mage::getModel(alias)
then get block type using $model->getInfoBlockType() - so you'll be able to find the actual Block by it's type
For example for ccSave payment method the info block is Mage_Payment_Block_Info_Ccsave, and template for it is app\design\frontend\base\default\template\payment\info\default.phtml. You'll be able to find all data inside those.
Good luck ;)
For the sake of completeness, exact functions to fetch CC type and last 4 digits of CC number are:
echo Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getCcType();
echo Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getCcLast4();
The block class is declared in layout update XML; see the onepage checkout and multishipping directives from checkout.xml. The actual child block which is used depends on the payment model which is being used, but there is a common template that will be used unless overridden.
Example:
See the generic CC method model Mage_Payment_ModelMethod_Cc
From that see its info block Mage_Payent_Block_Info_Cc...
...which will lead you to the "base" payment info block Mage_Payment_Block_Info which sets a default template.

How do I get the suffix (in code) that is being used for urls?

Magento can add a suffix that is defined by the user to append onto urls. I want to get that suffix from my code. Does anyone know an easy way to do this?
If it's stored in the configuration area, then you access it just as you would any other configuration value, by using Mage::getStoreConfig($config_path) where $config_path is defined in the system.xml of the module that defines it.
If you're not sure of the $config_path, then I usually cheat and inspect the textbox/dropdown in the configuration section, take a look at the id, e.g. dev_log_file, and translate it to dev/log/file. You'll need to use some intelligence when there are multiple _ though :)
Nick's answer is good but the actual answer to this question is:
$suffix = Mage::helper('catalog/category')->getCategoryUrlSuffix();
If I am not mistaken, here is the code ( because I don't understand what you want with URL )
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
$url_parts = split('[/.-]', $currentUrl); // escape characters should change based your url
echo $url_parts[0]; //check here
?>
complete product url:
$productId = ***;
$productUrl = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();

Resources