Magento 2 Image Selector in Widget CMS section - magento2

I've created a Magento 2 widget with an image chooser (among other things). When I use this widget in the CMS and add it straight to a page (Content > Pages > Edit Page > Insert Widget), the image chooser will show up correctly, and allow me to select or upload an image.
However, if I go into the Widgets section of the CMS (Content > Widgets > Add Widget), trying to use the same image chooser part of the Widget gives Uncaught ReferenceError: MediabrowserUtility is not defined.
In the searching that I've done, it seems that it's due to TinyMCE not being loaded on this page, but I can only find code references to fix it in Magento 1.
If that is the case, how would I get the right javascript file included on the Widgets page in the CMS?

i have found this solution:
In the block of the Uploader (Ex: Company/Module/Block/Adminhtml/Uploader), the method prepareElementHtml(AbstractElement $element):
$config = $this->_getData('config');
$sourceUrl = $this->getUrl('cms/wysiwyg_images/index', ['target_element_id' => $element->getId(), 'type' => 'file']);
$chooser = $this->getLayout()->createBlock(Button::class)
->setType('button')
->setClass('btn-chooser')
->setLabel($config['button']['open'])
->setOnClick('MediabrowserUtility.openDialog(\'' . $sourceUrl . '\', 0, 0, "MediaBrowser", {})')
->setDisabled($element->getReadonly());
$input = $this->elementFactory->create("text", ['data' => $element->getData()]);
$input->setId($element->getId());
$input->setForm($element->getForm());
$input->setClass("widget-option input-text admin__control-text");
if ($element->getRequired()) {
$input->addClass('required-entry');
}
$element->setData('after_element_html', $input->getElementHtml() . $chooser->toHtml() . "<script>require(['mage/adminhtml/browser']);</script>");
return $element;
Here is an example.

Related

custom WordPress tag.php page .. trying to get tag info

I'm creating a custom tag.php page for a WordPress theme.
I need to get the name, id and slug for the tag page that I am one (i.e. www.site.com/tag/fruit/ )... I see tons of examples online about how to list the tags for a page/post, but none that say how to easily get the name, id and slug for a specific tag page.
I tried to use the following... but the code below is for cycling through all the tags in a post. I just need the one tag for that tag page. If I try the option below, I don't get the correct tag (it picks some other tag on my site).
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag_id = $tag->term_id;
$tag_name = $tag->name;
$tag_slug = $tag->slug;
}
}
echo "<!-- tag_id: ".$tag_id." -->";
echo "<!-- tag_name: ".$tag_name." -->";
echo "<!-- tag_slug: ".$tag_slug." -->";
Any help is appreciated. Thanks
On your tag page, and for that matter, any taxonomy/term page, you can get the info from the current term being viewed with get_queried_object(). This will return the whole term object.
If you just need the ID, you can use get_queried_object_id()
Example:
$term = get_queried_object();
var_dump( $term );

How to remove a text field on a CGI perl webpage?

I am rewriting this question to make it specific:
The requirement of the webpage is
Read the rules from a file and populate the text-boxes when the webpage is loaded
An add button to add additional textboxes
A save button to save the rules ( including the additionally added ones back to the file)
A delete button to delete the textboxes which are checked so that the rules in them are not stored back to the file.
My approach :
Use "read_rules" submodule to read the rules from the webpage and if its non empty then use "rule" submodule to print them on webpage. Here each line in the file is read and split with spaces and put in a array of hash and prints on the webpage ("rules" module).
When the sumit button is pressed sumodule "save_rules" is called which saves the rules. The reverse way as in read_rules.
An add button will add additional text areas.
A delete button should delete the text areas which are checked and reload the page so when the page is reloaded the text areas which were checked wont appear and hence when save button is pressed they are not saved in file ??? (this I have to implement)
The code blocks of each submodule I have mentioned are below.
This submodule to print the text boxes and checkbox:
sub rule {
my($num,$rule) = #_;
return join "\n",
"<tr><td><br>",
hidden(
-name => "idx$num",
-default => $rule->{idx},
),
checkbox(
-name => "checkbox$num",
-label => "",
-value => "on",
),
"<td>",
textfield(
-name => "repository$num",
-size => 30,
-default => $rule->{'repo'},
),
"<td>",
textfield(
-name => "branch$num",
-size => 30,
-default => $rule->{'branch'},
),
"<td>",
textfield(
-name => "file$num",
-size => 30,
-default => $rule->{'file'},
),
}
This sumodule to read the rules from file
sub read_rules {
my $rule;
#rules = ..# read the rules from a file, each line is read as an element of an array
for $rule (#rules){
my $rec = {};
($re,$br,$fi) = split (' ', $rule);
$rec->{'repo'} = $re;
$rec->{'branch'} = $br;
$rec->{'file'} = $fi;
push #config, $rec;
}
}
The sumodule to save the rule on webpage ( in text area) back to the file
sub save_rules {
for ($i = 1; param("repository$i"); $i++) {
$repo1 = param("repository$i");
$branch1 = param("branch$i");
$file1 = param("file$i");
my $record = {};
# save back $myrec->{'repo1'} $myrec->{'branch1'} $myrec->{'file1'} to file
}
}
Main function
print start_form();
read_rules();
Delete_all();
my $i = 0;
if (#config) {
foreach $conf (#config) {
$i++;
print rule($i,$conf);
}
print $table_header_string;
if (param("action") eq "Add") {
print(br,rule(++$i),hr);
}
print p( submit (-name => "action" , -value => "Add"));
print p( submit (-name => "action" , -value => "Save"));
print p( submit (-name => "action" , -value => "Delete"));
print end_form();
exit 0;
Delete_rule submodule below has to be coded !!
sub delete_rule(){
#................
}
I would appreciate if some one tells me how to use javascript in cgi so I don't have to load webpage every time a button is pressed.
Edited to address more specific question
Given that you don't want to reload the page each time the button is clicked, you are going to want to use JavaScript to do this. I'll explain how here, and you can click here to see a working jsfiddle example.
<!doctype html>
<html>
<head>
<title>Remove Element On Button Click</title>
</head>
<body>
<input type="text" id="myTextBox" value="something here">
<input type="button" id="btnDelete">
</body>
<script>
document.getElementById('btnDelete').onclick = function ()
{
var textbox = document.getElementById('myTextBox');
textbox.parentNode.removeChild(textbox);
}
</script>
</html>
Notice that I changed the type of input element from submit to button. This automatically prevents the page from reloading.
If you absolutely must use a submit input element (and the jsFiddle example does), you can prevent form submission by changing the Javascript thusly:
document.getElementById('btnDelete').onclick = function (evt)
{
if (!evt) var evt = window.event;
if (evt.preventDefault) { evt.preventDefault(); } else { evt.returnValue = false; }
var textbox = document.getElementById('myTextBox');
textbox.parentNode.removeChild(textbox);
}
The JavaScript can be placed in an external file, if prefered. It's placed at the end of the HTML here so as to ensure that the DOM had fully loaded (and hence the button was available in the DOM) prior to attempting to attach an event handler.
It was unclear to me whether there would be one or multiple 'Delete' buttons in your output, or if there would be one or multiple fields that would need to be removed, but this should point you in the right direction.
This is a pure JavaScript solution. As #vol7ron correctly pointed out, there are JavaScript frameworks, like jQuery, that could be used to do this as well, and if you are already using such a framework it would be advantageous to utilize it, however no such framework is in anyway required to accomplish this.

TYPO3: How to render translated content in extension

I am developing a TYPO3 6.0 plugin that shows the subpages of the current page as tabs. For example, on the following pages my plugin is inserted on TabRoot:
If TabRoot is requested, the plugin's ActionController looks up the database for the subpage titles and contents and passes all gathered data to a Fluid template. The page is then rendered like the following:
With JS in place I always hide/show content below based on the selection. My problem is that I want to show the translated content of the subpages based on the current language selection. How am I able to do this? I've tried it with several methods, but neither of them was flawless. These are the methods I've tried:
Using RECORDS This method is not affected by the selected language, it always returns the content in the default language:
//Get the ids of the parts of the page
$select_fields = "uid";
$from_table = "tt_content";
$where_clause = 'pid = ' . $pageId;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
$select_fields,
$from_table,
$where_clause,
$groupBy='',
$orderBy='sorting',
$limit=''
);
$ids = '';
$firstIteration = true;
while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
if (!$firstIteration) $ids .= ",";
$ids .= $row ['uid'];
$firstIteration = false;
}
$GLOBALS['TYPO3_DB']->sql_free_result( $res );
//Render the parts of the page
$conf ['tables'] = 'tt_content';
$conf ['source'] = $ids;
$conf ['dontCheckPid'] = 1;
$content = $this->cObj->cObjGetSingle ( 'RECORDS', $conf );
Using CONTENTS According to TYPO3: How to render localized tt_content in own extension, this is the way to do it, however for me this also returns the content rendered with the default language. It is not affected by a language change.
$conf = array(
'table' => 'tt_content',
'select.' => array(
'pidInList' => $pageId,
'orderBy' => 'sorting',
'languageField' => 'sys_language_uid'
)
);
$content = $this->cObj->cObjGetSingle ( 'CONTENT', $conf );
Using VHS: Fluid ViewHelpers I installed the vhs extension and tried to render the content with <v:content.render />. The result is the same as with CONTENTS; it only works with the default language.
{namespace v=Tx_Vhs_ViewHelpers}
...
<v:content.render column="0" order="'sorting'" sortDirection="'ASC'"
pageUid="{pageId}" render="1" hideUntranslated="1" />
Using my own SQL query I've tried to get the bodytext fields of the page and then render those with \TYPO3\CMS\Frontend\Plugin\AbstractPlugin::pi_RTEcssText(). This method returns the content based on the current language, however the problem is that bodytext's do not contain the complete content (images, other plugins, etc).
$select_fields = "bodytext";
$from_table = "tt_content";
$where_clause = 'pid = ' . $pageId
. ' AND sys_language_uid = ' . $GLOBALS ['TSFE']->sys_language_uid;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
$select_fields,
$from_table,
$where_clause,
$groupBy='',
$orderBy='sorting',
$limit=''
);
$content = '';
while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
$content .=
\TYPO3\CMS\Frontend\Plugin\AbstractPlugin::pi_RTEcssText( $row ['bodytext'] );
}
$GLOBALS['TYPO3_DB']->sql_free_result( $res );
What am I missing? Why isn't the content rendered with the current language in the case of the CONTENTS method?
Easiest way is to use the cObject viewhelper to render right from TypoScript.
And inside your TypoScript template provide the configuration:
lib.myContent = CONTENT
lib.myContent {
...
}
BTW, you are bypassing the TYPO3 CMS API. Please do not do so. Always use the API methods to query for data.
e.g. \TYPO3\CMS\core\Database\DatabaseConnection is always available at GLOBALS['TYPO3_DB']->. Do not use the the mysql function.
On top of that, I believe that you can archive whatever you are trying to do with pure TypoScript, without the need to program anything. Feel free to ask a new questions to get help on this.
In TYPO3 4.x you could use the following methods to load the translated record:
t3lib_pageSelect->getRecordOverlay
t3lib_pageSelect->getPageOverlay
They are also available at $GLOBALS['TSFE']->sys_page->getRecordOverlay().

Add the product to cart using WWW::Mechanize - Perl

I'm writing a script that selects a size and adds the product to cart here is where it is
http://store.nike.com/us/en_us/pd/free-4-flyknit-running-shoe/pid-1064825/pgid-1481072
use WWW::Mechanize::Firefox;
$mech = WWW::Mechanize::Firefox->new();
my $tconike = "http://store.nike.com/us/en_us/pd/free-4-flyknit-running-shoe/pid-1064825/pgid-1481072";
$mech->get($tconike);
print $mech->uri();
$mech->submit_form(
form_number=> 2,
fields => {
skuAndSize => $shoesize,
click => "ADD TO CART",
}
);
But here is the output
Uncaught exception from user code:
No form found to submit. at nikecartstandalone.pl line 25
at C:/Users/Brett/Documents/brett/Perl/perl/site/lib/WWW/Mechanize/Firefox.pm l
ine 2162
WWW::Mechanize::Firefox::signal_condition('WWW::Mechanize::Firefox=HASH(
0x2a54888)', 'No form found to submit.') called at C:/Users/Brett/Documents/bret
t/Perl/perl/site/lib/WWW/Mechanize/Firefox.pm line 3649
WWW::Mechanize::Firefox::submit_form('WWW::Mechanize::Firefox=HASH(0x2a5
4888)', 'form_number', 2, 'fields', 'HASH(0x3501328)') called at nikecartstandal
one.pl line 25
Anyone know what I did wrong, is it because I should have used something besisdes submit_form or is it something else?
As the error says it's not able to find the form, so try submitting form with with_fields where you can specify the fields which are there in the form you are going to submit, that'll be easier to search and submit.
Eg: $mech->form_with_fields('username');
will select the form that contain a field named username.

In(Mazento) Zend..How Can I Pass String(Some Text) from Controller to View in Redirect URL

I used Like this But This is not Working
$msg=array('id' =>'Account Sucessfully Deleted');
$this->_redirect('customer/account/dresshoheaccount',$msg);
i want $msg Print in My Page,
you can easily do this as follows :
Mage::getSingleton('catalog/session')->addSuccess($this->__('Account Sucessfully Deleted'));
Please note that insert this line before you laid your layout.
you can call this is template as :
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
If not helping, try to add messages not to catalog/session, but to core/session.