Magento configuration - populating field when using frontend_model - configuration

I have specified a frontend_model in system.xml for an entry field. I did this because I wanted to make a field read-only. There may have been a more straightforward way to achieve that, but that's where I am at the moment. The thing is I cannot get the field to display the data it should.
I have a button that when pressed, populates the read-only field. That works fine. But when I hit 'Save Config', the data disappears from the field. The reason it disappears is because I can't find out what I should set the field's value to. Below tries using the getEscapedValue() method of Varien_Data_Form_Element_Abstract, but it returns nothing. And as usual with Magento, there is no documentation to speak of.
class Mypackage_MyModule_Block_Adminhtml_System_Config_DisabledText extends Mage_Adminhtml_Block_System_Config_Form_Field
{
protected function _prepareLayout()
{
parent::_prepareLayout();
if (!$this->getTemplate()) {
$this->setTemplate('mypackage/system/config/disabled_text.phtml');
}
return $this;
}
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$originalData = $element->getOriginalData();
$this->addData(array(
'my_value' => $element->getEscapedValue(),
'html_id' => $element->getHtmlId(),
));
return $this->_toHtml();
}
}
disabled_text.phtml contains the following:
<input id="<?php echo $this->getHtmlId() ?>" value="<?php echo $this->getMyValue(); ?>" class=" input-text" type="text" disabled/>
Thanks.

This is one of those places where you need to look at how Magento itself is doing something similar to what you want to do. If you look at the Mage_Adminhtml_Block_System_Config_Form_Field class's _getElementHtml
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
return $element->getElementHtml();
}
you can see that this method accepts a form element that's already been instantiated (based on what's in system.xml), and then this element renders itself with getElementHtml. That means when Magento needs to render (and, in turn, get the value) it does so in from the element object. Some crude debugging will let us know where getElementHtml can be located
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
var_dump(get_class($element));
return $element->getElementHtml();
}
Something like Varien_Data_Form_Element_Text will be dumped to the screen. In turn, this class inherits form Varien_Data_Form_Element_Abstract, which contains the following definition
public function getElementHtml()
{
$html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
.'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
$html.= $this->getAfterElementHtml();
return $html;
}
So, when Magento wants to get the value for a system config field, it uses the above PHP code to render the input. So, if you want to do the same in your template, I'd try something like this
Up in the class, assign a block property with the entire element. This is actually more efficient that plucking values out of the elements, since all PHP needs to store is an object reference.
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$this->setMyElement($element);
return $this->_toHtml();
}
Then, in the template, copy the code from magento's rendering, replacing the "$this" keyword with your saved element
<?php $_element = $this->getMyElement(); ?>
<!-- check the quoting/escaping on this html/php, I didn't actually run it, but the concept is sound -->
<input disabled="disabled" id="<?php echo $_element->getHtmlId();?>" name="<?php echo $_element->getName();?>"
value="<?php echo $_element->getEscapedValue();?>"
<?php echo $_element->serialize($_element->getHtmlAttributes());?>
/>
<?php echo $_element->getAfterElementHtml(); ?>
When you're working with Magento, try to think like a Magento developer. Instead of "I need to figure out how to make it do X", think "I need to add this feature to the store in the same way the rest of my teammates have". Then look how the core team did it, and copy their implementation, changing as little as you need to.
It does get easier the more you work with the system!

Related

why the submit button is not working in laravel

I tried to edit the data form but the submit button doesn't work like it doesn't have a function. In that form I also added 1 more form which was wrapped in a modal
code of view:
code of controller:
public function update(Request $request)
{
$items = Arsip::where('field_id', Auth::user()->employee->field_id)->get();
$request->validate([
'indikator'=>'required',
]);
$i = $request->indikator;
foreach($i as $row) {
$i = indicator::find($row['id']);
$i->arsip_id = $row['arsip_id'];
$i->nama_indikator = $row['nama_indikator'];
$i->nilai_indikator = $row['nilai_indikator'];
$i->save();
}
return view('admin.approve.index', compact('items'))->with('success', 'update data successfully.');
}
Don't use images in questions
You're not very clear on what is or isn't working. It feels like your form action isn't correct. $items, seeing your code snippet` is a collection and not a single model, which would mean the route wouldn't be correct.
I would suggest using named arguments when building a route: route('arsip.update', ['id' => $item->id])
Also, you're nesting a form inside a form. This is not allowed within HTML, and there is no good reason for wanting to do this. I see that this is a delete button. You can hide a form somewhere else and use javascript or a button (outside a form) with a form attribute to submit the form. See this thread's answers for this approach.

Yii2 mpdf with barcode-generator

I'm using yii2 and installed two extension : mpdf , and yii2-barcode-generator-8-types.
Both of them were installed and configured properly and working well.
But what I can't do is to load barcode into pdf.
Here is my code :
Controller :
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
echo $this->renderPartial('_pdf');exit;
return $pdf->render();
}
View :
<div id="showBarcode"></div>
<?php
use barcode\barcode\BarcodeGenerator as BarcodeGenerator;
$optionsArray = array(
'elementId'=> 'showBarcode',
'value'=> '12345678',
'type'=>'code128',
);
echo BarcodeGenerator::widget($optionsArray);
?>
And it show something like this
but If I try to delete all code inside actionPdf() and just write
return $this->render("_pdf");
it show like this:
Please help!!!
I think your controller should be this
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
return $pdf->render();
}
The row with echo $this->renderPartial('_pdf');exit; don't must be used because it prevents the program to invoke correctly the render of the pdf page. If you use this instruction you displays only the render of "html like code" page like you see in result you posted moreover immediately after this instruction you exit form action without invoking the $pdf->render.

How should I use requestAction in the view with CakePHP 3.x

My code:
// View/Activities/index.ctp
...
<div>
<?php echo $this->requestAction('/Activities/ajax_list/'.$categoryId,['return']);?>
</div>
...
//View/Activitest/ajax_list.ctp
....
<?php echo $this -> Html -> image("/img/add1.jpg"); ?>
...
<?php echo $this->Html->link('add_project', array('controller'=>'projects', 'action'=>'add', $categoryId)); ?>
....
I want to include the view 'ajax_list' into the 'index',and it has been displayed but the url of image and link was wrong.
Then I debug the Cake/Routing/RequestActionTrait.php , "requestAction" function I find "$request = new Request($params);" the $request->base , $request->webroot are null.
Could some one tell me how should I fix it?
The $base/$webroot properties not being set in the new request might be considered a bug, or maybe the docs are just missing a proper example solution, I can't tell for sure, you might want to report this over at GitHub and see what the devs say.
Use view cells instead of requestAction()!
Whenever applicable you're better off using view cells instead of requesting actions, as they avoid all the overhead that comes with dispatching a new request.
See Cookbook > Views > View Cells
No models involved? Use elements!
In case no models are involed, and all you are doing is generating HTML, then you could simply use elements.
See Cookbook > Views > Elements
Fixing requestAction()
A possible workaround would be to use a dispatcher filter that fills the empty $base/$webroot properties with the necessary values, something like
src/Routing/Filter/RequestFilterFix.php
namespace App\Routing\Filter;
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
class RequestFixFilter extends DispatcherFilter
{
public function beforeDispatch(Event $event)
{
$request = $event->data['request'];
/* #var $request \Cake\Network\Request */
if ($request->param('requested')) {
$request->base = '/pro';
$request->webroot = '/pro/';
$request->here = $request->base . $request->here;
}
}
}
config/bootstrap.php
// ...
// Load the filter before any other filters.
// Must at least be loaded before the `Routing` filter.
DispatcherFactory::add('RequestFix');
DispatcherFactory::add('Asset');
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
// ...
See also Cookbook > Routing > Dispatcher Filters > Building a Filter

i want to fetch same template in cntroller

my index controller this and showing good html and header and footer is working correct but when i want to add method the css and html not working .
public function index()
{
$data['clients_list'] = $this->clients_model->get_all_clients();
$this->load->view('template/header');
$this->load->view('clients/index', $data);
$this->load->view('template/footer');
}
this is my add method where i want to add html as header and footer
public function add()
{
$this->load->view('template/header');
$this->load->view('clients/add');
$this->load->view('template/footer');
}
The way you are using $this->load->view() is not supported in the framework. You should only be loading one view. If you wish to include multiple views within a single view you must save them within the data to be rendered in the master view and echo them out within that view. To do this you use the third parameter of the view function explained at the codeigniter api: Very bottom of page
An example of this would be:
Controller:
function index(){
$data['clients_list'] = $this->clients_model->get_all_clients();
$data['header'] = $this->load->view('template/header', $data, TRUE);
$data['footer'] = $this->load->view('template/footer', $data, TRUE);
$this->load->view('clients/index', $data);
}
View:
<?php echo $header; ?>
<!-- YOUR INDEX PAGE CONTENT-->
<?php echo $footer; ?>
The third parameter of view() tells the framework to pre-render the view and output the contents as a string value which allows you to save as a variable and echo into the primary page.
Phil Sturgeon wrote a wonderful template library that I have been using in my projects that allows for a webforms style "master page" that you can have the rest of your views rendered inside of so that you do not have to build your header/footer view each time and inject it into every view.
Phil Sturgeon Template Library

Sending values through links

Here is the situation: I have 2 pages.
What I want is to have a number of text links(<a href="">) on page 1 all directing to page 2, but I want each link to send a different value.
On page 2 I want to show that value like this:
Hello you clicked {value}
Another point to take into account is that I can't use any php in this situation, just html.
Can you use any scripting? Something like Javascript. If you can, then pass the values along in the query string (just add a "?ValueName=Value") to the end of your links. Then on the target page retrieve the query string value. The following site shows how to parse it out: Parsing the Query String.
Here's the Javascript code you would need:
var qs = new Querystring();
var v1 = qs.get("ValueName")
From there you should be able to work with the passed value.
Javascript can get it. Say, you're trying to get the querystring value from this url: http://foo.com/default.html?foo=bar
var tabvalue = getQueryVariable("foo");
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
}
** Not 100% certain if my JS code here is correct, as I didn't test it.
You might be able to accomplish this using HTML Anchors.
http://www.w3schools.com/HTML/html_links.asp
Append your data to the HREF tag of your links ad use javascript on second page to parse the URL and display wathever you want
http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
It's not clean, but it should work.
Use document.location.search and split()
http://www.example.com/example.html?argument=value
var queryString = document.location.search();
var parts = queryString.split('=');
document.write(parts[0]); // The argument name
document.write(parts[1]); // The value
Hope it helps
Well this is pretty basic with javascript, but if you want more of this and more advanced stuff you should really look into php for instance. Using php it's easy to get variables from one page to another, here's an example:
the url:
localhost/index.php?myvar=Hello World
You can then access myvar in index.php using this bit of code:
$myvar =$_GET['myvar'];
Ok thanks for all your replies, i'll take a look if i can find a way to use the scripts.
It's really annoying since i have to work around a CMS, because in the CMS, all pages are created with a Wysiwyg editor which tend to filter out unrecognized tags/scripts.
Edit: Ok it seems that the damn wysiwyg editor only recognizes html tags... (as expected)
Using php
<?
$passthis = "See you on the other side";
echo '<form action="whereyouwantittogo.php" target="_blank" method="post">'.
'<input type="text" name="passthis1" value="'.
$passthis .' " /> '.
'<button type="Submit" value="Submit" >Submit</button>'.
'</form>';
?>
The script for the page you would like to pass the info to:
<?
$thispassed = $_POST['passthis1'];
echo '<textarea>'. $thispassed .'</textarea>';
echo $thispassed;
?>
Use this two codes on seperate pages with the latter at whereyouwantittogo.php and you should be in business.