Drupal 8 : more suggestions for input forms - php

I'm making a form and when I want to custom my input, I can see only one suggestion already used for another form .
There is a solution to have others suggestion without module if it's possible?
I'm using this function to have input suggestions:
function terreal_theme_suggestions_input_alter(array &$suggestions, array &$variables, $hook)
{
if (isset($variables['element']['#id'])) {
$id = str_replace("-", "_", $variables['element']['#id']);
$suggestions[] = $hook . '__' . $id;
}
}
my suggestion used is : input--edit-field-lastname-0-value.html.twig
thanks

Related

Adding script to certain nodes and pages on drupal 7

I am trying to add a script code on all pages of my drupal except a few. Is there any conditionals I can use in html.tpl.php ? or any functions on the template.php to achieve this?
I tried the code below on template.php but no luck :(
Thanks in advance!
function THEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__' .
$variables['node']->type;
}
//this is what I am trying
if ($variables['nid'] == '77') {
drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
}
}
You can use hook_page_build(&$page)
function MYMODULE_page_build(&$page){
$nids = array(123, 1234); // your nids
if(($node = menu_get_object('node', 1)) && in_array($node->nid ,$nids) ){
drupal_add_js(drupal_get_path('theme', 'mythemename') .'/scripts/path.js');
}
}
Clear all cache after create this function to see result , also ensure of path script is correct ;)

Function Call and variables problems

I would like to ask a question about php . That's related php function , there are two different function but whenever call this function , doesnt work below highlight(Doesnt Work Area) place .By the way , I tried call content variable from out of function and also I wrote global variable but I couldnt.
Do you know , why ?
Java () {
$res=mysql_query("SELECT * FROM candidate WHERE candidate_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
if(isset ($_POST['language'])) {
$deneme=$_POST['language'];
}
$file= $userRow['candidate_name'];
touch($file.'.java');
if ( isset ($_POST ['content']) )
{
file_put_contents ($file.'.java',$_POST ['content']); **Doesnt Work HERE!**
}
exec("C:\Java\java\bin\javac -verbose $file.java 2>&1" , $output, $resultCode);
.
.
.
.
.
.
}
Is there any advice about it ?
Thank you in advance
Do you sure that if ( isset ($_POST ['content']) ) is true?
Check write permissions
Execute after writing.
Instead of
Java () {
You should use:
function Java() {
// Function contents...
}

Get Contact Form 7 Form ID In PHP

I'm using contact form 7 to load two different forms into a page and then, in addition to sending the email, dynamically adding that information to a database. Unfortunately, because of the plugin, I can't simply just create all inputs with different names to avoid needing a filter. So, essentially, I'd like to pull the form ID into the action hook and dynamically create the $data variable based on which form is being submitted, but I'm not sure how to get the cf7 form ID. Does anyone know how to accomplish this, or perhaps a more feasible way of doing it?
Form Shortcodes
[contact-form-7 id="221" title="Reg 1"] [contact-form-7 id="112" title="Reg 2"]
PHP Action Hook in functions.php
function save_form( $wpcf7 ) {
global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if($form_to_DB) {
$formData = $form_to_DB->get_posted_data();
}
if("Request a Free Demo" != $formData['demo_request'][0]){
$freeDemo = "yes";}else { $freeDemo = "nope";}
if(THE FORM ID = 221) {
$data = array(
some values from the 112 form
$wpdb->insert( $wpdb->prefix . 'registrations', $data );
);
}elseif(THE FORM ID = 112) {
$data = array(
some other values from the 112 form
$wpdb->insert( $wpdb->prefix . 'registrations_2', $data );
);
}
}
remove_all_filters('wpcf7_before_send_mail');
add_action('wpcf7_before_send_mail', 'save_form' );
I tend to use the "wpcf7_posted_data" action hook (though this might have changed as the question is a bit old now). You don't need to remove all filters.
For example:
function processForm($cf7)
{
$wpcf7 = WPCF7_ContactForm::get_current();
$form_id = $wpcf7->id;
if ($form_id === 221)
{
//Do Stuff
}
else if ($form_id === 112)
{
//Do different stuff
}
}
add_action("wpcf7_posted_data", "processForm");
$wpcf7->idis no longer accessible, use $wpcf7->id() instead.
simply use this:
function save_form( $wpcf7 ) {
if($wpcf7->id == 4711) {
// whatever
}
}
You can use this: $form_id = $_POST['_wpcf7'];
SOLVED:
I wound up just using a logical operator to check if a form specific field was empty or not. If the field "form_2_name" was empty when a form was submitted, then we know that form 1 is being submitted. Simple if statement with that logic did the trick!

In the Ninja Forms Webhooks extension, is it possible to intercept the submit without editing the plugin's process() function?

I am attempting to edit a form's submission variables before they are submitted to the action URL. I am able to accomplish what I want by changing
$args[ $vars['key'] ] = $value;
in the initial foreach loop of the process() function in action-webhooks.php to
if (is_array ($value)) {
$args[ $vars['key'] ] = implode(';', $value);
}
else {
$args[ $vars['key'] ] = $value;
}
This is obviously a bad solution since this code will be overwritten if the plugin is updated. Is there an action or filter that I can use to accomplish this from my own code? Thanks.
From what I can tell, it looks like using the ninja forms processing method you can set the variables before the form is completely processed.
global $ninja_forms_processing;
//get the form id
$form_id = $ninja_forms_processing->get_form_ID();
//The id of the form you want to check
$form_to_check = 5;
if($form_id == 5)
{
//What the user entered
$user_value = $ninja_forms_processing->get_field_value( 3 );
if(is_array($user_value))
{
//set the value to what it you want it to be
$new_value = implode(';', $user_value);
$ninja_forms_processing->update_field_value( 3, $new_value );
}
}

Zend framework 2: Form bind not setting the values extracted from database

I want to create a form with Zend Framework 2 for my application and I have everything in place and the form is getting displayed but my issue is I am not able to bind the initial form values that come from the database
$myUserDetails = <details of my user coming from DB>;
$form = $form->bind($myUserDetails); //This should set the values for the form to display but it doesnt
My display logic is simple as shown below
$form = $this->form;
$form->setAttribute('action',
$this->url('<routename>',array('action'=>'<actionname>')));
$form->prepare();
echo $this->form()->openTag($form) . PHP_EOL;
echo $this->formRow($form->get('email_id')) . PHP_EOL;
echo $this->formRow($form->get('dob')) . PHP_EOL;
echo $this->formRow($form->get('gender')) . PHP_EOL;
echo $this->formRow($form->get('user_page_name')) . PHP_EOL;
echo $this->formInput($form->get('submit')) . PHP_EOL;
echo $this->form()->closeTag($form) . PHP_EOL;
Now I tried to set the data from my object that I bind to the form in my controller action
$myUserDetails = <details of my user coming from DB>;
$form = $form->bind($myUserDetails);
$data = $myUserDetails->getArrayCopy();
$form->setData($data['data']);
This seems to work somehow and is displaying my values in the view. So i just want to know what have I done wrong in my first approach? Thanks in advance :)
Thanks to #Stoyan Dimov I solved the issue. Basically it was with getArrayCopy function in my model as I was direcly returning an array which was enclosed in an index 'data'
So I changed my getArrayCopy to
public function getArrayCopy() {
$data = get_object_vars($this);
return $data['data'];
}
And voila it worked.
Thanks again Stoyan Dimov

Categories