Render comments form in any page - forms

I want to know how to render a comments form of a specific node in any page (ex. user profile). I've trying with drupal_get_form but it shows an error.
drupal_get_form('mytype_node_form', array('nid' => $nid));
Solutions & clues are welcome :)

First of all, you should use the proper ID of the comment form: 'comment_form' instead of 'mytype_node_form'.
The code
drupal_get_form('comment_form', array('nid' => $nid));
used to work for me in Drupal 6. In Drupal 7, function comment_form() is expecting an object parameter instead of array. This code should work for you:
$comment = new stdClass;
$comment->nid = $nid;
$form = drupal_get_form('comment_form', $comment);

Related

Magento 2 with Full Page Cache: How to get product ID from a product page?

I am trying to find a solution to what seems to be a FPC-linked issue.
In my code I am using the following method in order to get the current product ID from a Product page.
$this->catalogSession->getData('last_viewed_product_id')
This worked just fine until I tried it on a website with Full Page Cache: in this case, it returns an empty value (maybe because the session data cannot be accessed from cache).
Does anyone know an alternative method to get the product ID from the current context?
I have tried this alternative synthax:
$_SESSION['catalog']['last_viewed_product_id'];
While not the best solution and definitely not best practice, you can use objectmanager:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$id = $product->getId();
This will get you the id but, as stated above, it's not suggested and you should create a block and inject the registry there and call it in your code.
You can see this post https://meetanshi.com/blog/get-current-product-id-in-magento-2/

Remove submitted state in submission handler

I am trying to reset a form so that it appears to Drupal 8 that it hasn't been submitted. So far I have been unable to do this, as I cannot find any available methods (setSubmitted() hardcodes it to TRUE without a FALSE option). The reason is this isn't a full submit, but a submit of one field after which I would like the user to be redirected to another page that has another form, and I would like this secondary form to use the value obtained in the first step.
In the submit handler for the first part I use this to redirect:
$form_state->setRedirect('my.route', [], []);
And this works, but when the form reaches the second form (it seems) that the second form thinks it is a submission. As a result any submit buttons I add to the second form seem to make it auto-submit, and this breaks my user journey.
In the submit for the first part I have tried:
$form_state->setRebuild(TRUE);
$form_state = new FormState();
unset($form_state);
Tried the above in various configurations to no avail. They all prevent/ignore the setRedirect call that I make afterwards. The reason I want/need to do it this way is I want to preserve the POST method used.
Do you want to obtain something similar to what core search module does? It has simple SearchBlockForm that sends data to more complex SearchPageForm.
SearchBlockForm uses GET method (though you may use POST):
$form['#method'] = 'get';
and has no id and token fields:
function search_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
}
BTW, the last change allows you to avoid running submit callbacks.
Hope this helps.

how to get static block content custom attributes by block id in magento

I am creating this below block content in admin HTML. I cant able to get the slidertype attributes in 3columns.phtml but in template page i can get
$this->getData('slidertype').
So kindly give the solution that how to get the following attributes.
{{block type="catalog/product_bestseller" name="bestseller" slidercount="20" slidertype="1" template="catalog/product/bestseller_right.phtml"}}
I am using the below code to get static block attribute value. Its working fine for us.
$bestSellerBlock = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load("best_seller");
$bestSellerContentText = strip_tags($bestSellerBlock->getContent());
preg_match('/slidertype="(.*?)"/u',$bestSellerContentText,$sliderTypeArray);
$sliderType = trim($sliderTypeArray[1]);
Thanks,
Arularasan D.

Atk4 Step by Step form doesn't load Facebook Like Buttons

I have a array with list of sites, I'm coding a step by step form using http://codepad.agiletoolkit.org/newsletter example.
In the second step I put Like Buttons using the following code:
$attr = array (
'data-send' => FALSE,
'data-layout' => 'button_count',
'data-width' => 100,
'data-show-faces' => FALSE
);
foreach($this->sites as $k => $site) {
$div = $form->add('View_HtmlElement')->setElement('div')->set(NULL);
$attr['data-href'] = $site;
$div->addClass('fb-like');
$div->setAttr($attr);
}
This works good when I access directly, but when I try to access via next button, the Like Buttons doesn't load.
Any solution for this?
The reason why the facebook and some other buttons may not work with AJAX pages is because facebook scripts generally process your HTML only during initial load of the page. When the form in your example goes to next step, it uses AJAX to load additional form. As a result you will need to either manually trigger facebook's scripts to re-walk your page or perform redirects instead of reload. You would need to change:
$this->js()->atk4_load($this->api->getDestinationURL('./step2'))
->execute();
to
$this->js()->univ()->location($this->api->getDestinationURL('./step2'))
->execute();
You may also find this article useful: http://agiletoolkit.org/blog/adding-twitter-button-to-ajax-page/

Multi-Page Form in Zend is Validating All Forms too early

I have been working through the Multi Page forms tutorial in the Zend Form Advanced Usage section of the documentation, http://framework.zend.com/manual/en/zend.form.advanced.html.
My first page loads fine, however when I submit it, the second page loads and it includes validation error messages. (Obviously I don't want to see validation errors for this page until the user has filled in the fields...)
I have tracked it down to the final line in the formIsValid() function. It seems that here validation is run for all elements in the three forms (not just the current one), so it's really no surprise that errors are showing on the second page.
I have tried the suggestion in the comments at the end of the tutorial, i.e. $data[$key] = $info[$key].
Have you had a crack at this tutorial? How did you solve the problem?
Any assistance is much appreciated!
I encountered the same problem this is how I solve it.
By replacing
public function formIsValid()
{
$data = array();
foreach ($this->getSessionNamespace() as $key => $info) {
$data[$key] = $info;
}
return $this->getForm()->isValid($data);
}
With
public function formIsValid()
{
$data = array();
foreach ($this->getSessionNamespace() as $key => $info) {
$data[$key] = $info[$key];
}
return (count($this->getStoredForms()) < count($this->getPotentialForms()))? false : $this->getForm()->isValid($data);
}
The documentations reads:
Currently, Multi-Page forms are not
officially supported in Zend_Form;
however, most support for implementing
them is available and can be utilized
with a little extra tooling.
The key to creating a multi-page form
is to utilize sub forms, but to
display only one such sub form per
page. This allows you to submit a
single sub form at a time and
validate it, but not process the form
until all sub forms are complete.
Are you sure you have been validating a single sub-form instead of just whole form?