Passing a variable from phtml to CMS block in Magento 2 - magento2

I have a need to sending data from block creation place in .phtml to my CMS block.
I create the block on the .phtml as follow
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?>
<?php if ($category->getShortDescription()) : ?>
<?php echo $block->getLayout()->createBlock(
'Magento\Cms\Block\Block')->setBlockId('short_description')->setData('sd','Hello Short')->toHtml();?>
<?php endif; ?>
Here, I'm passing a parameter to the block as setData('sd','Hello Short') But the parameter is not showing on the cms block
I call this parameter/argument on my blog as
The Short Description is {{sd}}
But I expect the output should be The Short Description is Hello Short but I The Short Description is {{sd}}

I have resolved this issue by replacing the text on CMS Block.
<?php if ($category->getShortDescription()) : ?>
<?php
$shortDescriptionBlock = $block->getLayout()->createBlock(
'Magento\Cms\Block\Block',"",["short_desc" => $category->getShortDescription()])->setBlockId('short_description')->toHtml();
echo str_replace("{{short_description}}", $category->getShortDescription(), $shortDescriptionBlock);?>
<?php endif; ?>

Related

Where is the file that operate <?php echo $this->getSubmitUrl($_product) ?> form action in Magento product view page

I see there is the following code in the app\design\frontend\base\default\template\catalog\product\view.phtml file:
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
Is there a file that operate this action action="<?php echo $this->getSubmitUrl($_product) ?>"?
What file?
In magento each '.phtml' file is associated with a block file which contain the available template method.
The block for template\catalog\product\view.phtml template is located in
/app/code/core/Mage/Catalog/Block/Product/View.php
class Mage_Catalog_Block_Product_View extends Mage_Catalog_Block_Product_Abstract
However getSubmitUrl($_product) is inherit from Mage_Catalog_Block_Product_Abstract
/app/code/core/Mage/Catalog/Block/Product/Abstract.php
public function getSubmitUrl($product, $additional = array())
{
...
}
Read more # Intro to Layouts

Form with embedded relation won't save Symfony 1.4 Doctrine

I have embedded the Mutt form within the Mix form:
MixForm.class.php:
$this->embedRelation('Mutt');
$form = new MuttForm(null, array(
'mix' =>$this->getObject(),
));
$this->embedForm('Mutt', $form);
$this->widgetSchema['Mutt'] = $form->getWidgetSchema();
$this->widgetSchema['Mutt']['mix_id'] = new sfWidgetFormInputHidden();
$this->validatorSchema['Mutt'] = $form->getValidatorSchema();
I need the newly created id form for the Mix table to populate the mix_id field in the Mutt table.
<?php echo $form->renderHiddenFields();?>
<?php echo $form['name']->renderRow();?>
<?php echo $form['parent1']->renderRow();?>
<?php echo $form['parent2']->renderRow();?>
<?php echo $form['parent3']->renderRow();?>
<?php echo $form['parent4']->renderRow();?>
<?php echo $form['parent5']->renderRow();?>
<?php echo $form['Mutt']['creator']->renderRow();?>
<?php echo $form['Mutt']['email']->renderRow();?>
<?php echo $form['Mutt']['website']->renderRow();?>
<?php echo $form['Mutt']['caption']->renderRow();?>
<?php echo $form['Mutt']['photo']->renderRow();?>
<?php echo $form['Mutt']['copyright']->renderRow();?>
<?php echo $form['Mutt']->renderHiddenFields();?>
Here is my action in modules/mix/actions/actions.class.php
public function executeEdit(sfWebRequest $request)
{
$this->form = new MixForm();
if($request->isMethod('post')):
$this->form->bind($request->getParameter('mix'), $request->getFiles($this->form->getName()));
if($this->form->isValid()):
$this->form->save();
$this->redirect('pure/add');
endif;
endif;
}
The form validation works correctly, but it won't save in either database.
What am I doing wrong??
You are defining an action for executeEdit, but processForm is the action where form validation and saving to the database occur. executeEdit is the action for displaying a form when editing an existing job.
See: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/10#chapter_10_sub_the_form_action

Zend_Dojo_Form in a layout

I have a Zend_Dojo_Form which I have moved from my view (where it works fine) to my layout, as it's something that will be useful on every page. However in the layout the form no longer works - none of the dijit elements appear and it behaves just as a normal HTML form would.
Here's the relevant part of my bootstrap:
protected function _initView()
{
Zend_Layout::startMvc(array(
'layoutPath' => '../application/layouts',
'layout' => 'default'
));
$view = new Zend_View();
$view->setEncoding('UTF-8')
->doctype('HTML5');
// init Dojo
Zend_Dojo::enableView($view);
$view->dojo()->enable()
->setCdnVersion('1.5')
->requireModule('dojo.data.ItemFileWriteStore')
[...]
->addStyleSheetModule('dijit.themes.tundra');
// assign the view to the viewRenderer, so it will be used by the MVC actions
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
return $view;
}
there are no errors (JS or ZF), the form just doesn't work as it should.
I assume I need to Dojo enable the Layout view in some way. I tried changing the layout part of the bootstrap method above to this:
$layout = Zend_Layout::startMvc(array(
'layoutPath' => '../application/layouts',
'layout' => 'default'
));
$view = $layout->getView();
Zend_Dojo::enableView($view);
$layout->setView($view);
but that didn't make any difference.
I found this question which sounds very similar to my problem, but the accepted answer just shows including the dojo helper in the layout, which I am doing already.
This is most probably due to that you have the layout as suggested in the docs:
<?php echo $this->doctype() ?>
<html>
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headMeta() ?>
<?php echo $this->headLink() ?>
<?php echo $this->headStyle() ?>
<?php if ($this->dojo()->isEnabled()){
$this->dojo()->setLocalPath('/js/dojo/dojo.js')
->addStyleSheetModule('dijit.themes.tundra');
echo $this->dojo();
}
?>
<?php echo $this->headScript() ?>
</head>
<body class="tundra">
<?php echo $this->layout()->content ?>
<?php echo $this->inlineScript() ?>
</body>
</html>
The problem is the echo $this->dojo() must be after the $this->form->render() otherwise the required modules won't have been registered in Zend_Dojo.

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);

how can strips html tags in joomla

hi i am working on joomla. and i used the latest news module but it emerging a problem that the new display on page having the tags like paragraph i wanna remove that tags from the news
my code is
<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<?php $i=1 ;?>
<?php foreach ($list as $item): ?>
<?php if($i==1) {echo "<div class='latestnews_ttl'> <p>".$item->cat_title." </p></div>";} $i++; ?>
<div class="news_box">
<p style=" padding:0px;"><strong><?php echo $item->text; ?> </strong></p>
<p><?php if(strlen($item->introtext)>100)
{
$txt = str_split($item->introtext, 100);
echo $txt['0']."...";
}
else{
echo $item->introtext;
}
?></p>
<div class='readmore'>read more</div></div>
<?php endforeach; ?>
It will give news with prefix tag "<p>" so please give the solution of it.
thanks in advance
Tried strip_tags() ?
Always remember in Joomla! never to modify core code, always overwrite the core templates by following the following guide http://docs.joomla.org/Tutorial_talk:Template_overrides
1: http://docs.joomla.org/Tutorial_talk:Template_overrides or a quick how to http://www.techportal.co.za/joomla/joomla-tutorials/124-how-to-modify-the-appearance-of-the-category-list-page-without-having-modifications-done-to-the-joomla-core-code
If you do this in the core it will work, but once you may upgrade your changes could be gone.
Since joomla is written in php you could use strip_tags