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

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/

Related

object to fluid.form on other page

Im trying to redirect from a list of entries to the edit page for these entries when an link.action is clicked.
I can't seem to get the values from the objects using the 'property' tag once I redirect to the editing page.
The action 'edit' is not getting executed on the page I redirect to. Instead it fires the standard action which is just listing all the entries.
public function toEditAction(Personenliste $person) {
$this->redirect('edit', 'Listen', 'testprivateext', ['personenliste' => $person], 43);
}
public function editAction(Personenliste $person) {
$this->view->assign('personenliste',$person);
return $this->htmlResponse();
}
The call is done via link.action. (I also tried directly redirecting the action with the 'pageUid'-tag)
<f:link.action action="toEdit" arguments="{person:'{person}'}" extensionName="testprivateext" controller="Listen" pluginName="pi1">&#128393</f:link.action>
This is not an answer to your question (because you already solved it), but a little info for more readable inline-fluid.
This:
arguments="{person:'{person}'}"
can be written:
arguments="{person: person}"
when you directly pass a variable, you can discard the quotes and curly braces.
but you will need it, when passing a VH:
arguments="{person:'{person -> f:format.raw()}'}"
Have a nice day :)

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.

Moodle 3.3 - check if a user is authenticated in Mustache template

I'm creating a mustache template for a Moodle site and want to display some content on the front page but only if the user is not yet logged in. I was hoping I could do something like this in the template:
{{^usernotloggedin}}
My content for users not logged in.
{{/usernotloggedin}}
However I can't find any documentation on the user variables available to mustache to test if a user is authenticated or not.
Any advice of where to look or how to implement this would be great.
You would need to adjust the code that calls the template to add that data to the context that is passed to the template. In that case you can use the isloggedin() function to set the value you want to pass to the template.
I hope I'm not quite late to this question by anyhow I'd like to add my answer to this as well
Your mustache files are simple templates which cannot perform logic. What you can do is
in your controller or the file which calls the render function (which renders the mustache file and gives HTML), apply a check there
require( '/path/to/moodle/config.php' );
if (isloggedin()) {
echo "you are logged in";
}
Once you have applied the check you can send the array with a flag identifier (ONLY TRUE or FALSE) which the mustache files can understand.
for eg
in your controller/block etc you can do the following
$tagcloud = core_tag_collection::get_tag_cloud($this->config->tagcoll, $this->config->showstandard == core_tag_tag::STANDARD_ONLY, $this->config->numberoftags, 'name', '', $this->page->context->id, $this->config->ctx, $this->config->rec);
$content = $tagcloud->export_for_template($OUTPUT);
require( '/path/to/moodle/config.php' );
$flag = isloggedin() ? TRUE : FALSE;
array_push($content, $flag);
$this->content->text = $OUTPUT->render_from_template('core_tag/search_course_by_tags', $content);
and in your mustache file
{{#flag}}
your fancy code here which will onyl work if the user is logged in
{{/flag}}

Render comments form in any page

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);

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?