How to add static html markup to a CiviCRM form - php

I have CiviCRM 4.4.6 + Drupal 7 and i alter one of CiviCRM's forms.
Inside hook_civicrm_buildForm(), i try to:
form->addElement('html', 'statichtml', '<div>aa</div>');
$template =& CRM_Core_Smarty::singleton();
$bhfe = $template->get_template_vars('beginHookFormElements');
if (!$bhfe) {
$bhfe = array();
}
$bhfe[] = 'statichtml';
$form->assign('beginHookFormElements', $bhfe);
If i use it with 'text' element type, it works correctly. This way nothing is rendered, but an empty additional tr is added.
How to use this type of element correctly?

http://pear.php.net/manual/hu/package.html.html-quickform.intro-elements.php
Here is the explanation.
The element type should be static, not html and the above code starts to work.

Related

Display image instead of text

I am very new in php. I have a site with the CMS joomla 3.6.
In the register of a third part component I have a select box to the members inform his gender. In the html of the page, returns the text 'male' and 'female'.
What I need is to replace the text 'male' and 'female' with images.
In the PHP file I have this:
public function getFieldData($field) {
$options = array("COM_COMMUNITY_MALE" => "COM_COMMUNITY_MALE", "COM_COMMUNITY_FEMALE" => "COM_COMMUNITY_FEMALE");
$value = strtoupper($field['value']);
if ( isset($options[$value])) {
return JText::_($options[$value]);
}else {
return '';
}
}
So, I know that the second "COM_COMMUNITY_MALE" and "COM_COMMUNITY_FEMALE" are the output html and I have tried to replace them with that code <img src="image/image.png"/>. But no luck, can someone tell me what is the path to do this?
Thank you in advance for your attention.
The problem is that you are using a dropdown box, and, for example, COM_COMMUNITY_MALE is the value and the label of one of the options in that dropdown box, and you can't have a value in a dropdown that is HTML code (for example, an img tag). You will need to review your requirements.

HTML Helper - Reusable View - PHP

I am new to PHP and I was hoping someone could help me determine what the best way to go about creating a reusable view would be. What I have is a form that will be used to Add or Edit data. Therefore the view will be identical except in the case of an Edit the form fields will be populated.
So I am creating an HTML helper that takes 2 parameters. The first will be form data (if there is any{edit}) and the second will be a bool that flags whether this is an insert or and edit(to change form action).
My question is... how should I handle the first parameter if the form is to used to Add data and therefore does not contain data? Optional parameter?
EDIT--
I am using CodeIgnitor as my MVC framework. That is what those form functions are being inherited.. fyi
Thanks..
<?php
if(!defined('BASEPATH') ) exit('No direct script access allowed');
if(!function_exists('WorkOrderForm'))
{
function WorkOrderForm($array = array('$query'),$edit)
{
$formHtml = "";
$attributes = array('class'=>'order','id'=>'orderForm');
if($edit)
{
$formHtml += form_open('order/update',$attributes);
}
else
{
$formHtml += form_open('order/add',$attributes);
}
$formHtml += form_input('name',$query[0]->name);
$formHtml += form_textarea('name',$query[0]->description);
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
$formHtml += form_dropdown('status',$dropOptions,$query[0]->status);
$formHtml += form_input('name',$query[0]->startDate);
$formHtml += form_input('name',$query[0]->endDate);
$formHtml += form_close();
return $formHtml;
}
}
?>
What are you guys doing? A reusable view is so much easier than this. Simply create a view and save it in the views folder. Add the fields that will appear both when adding and editing data and use an if statement in the value parameter to determine if it has data.
E.g.
Controller:
public function add()
{
$data['method'] = 'add';
$data['form_data'] = $this->some_model->get_something();
$this->load->view('reusable_view', $data);
}
public function edit($id)
{
$data['method'] = 'edit';
$data['form_data'] = $this->some_model->get_something($id);
$this->load->view('reusable_view', $data);
}
View:
<form method="post" action="my_controller/" . <?php echo $method; ?>>
<input type="text" value="<?php if ( isset($form_data['something']) ) {echo $form_data['something'];} " />
</form>
I see no reason to populate a form in a controller as that's not the way MVC works. Using a helper in order to populate the form is also weird, I think you've slightly missed the point of how Codeigniter works.
First off, the default argument should be on the right. And I would default it to 'false' or NULL.
function WorkOrderForm($edit, $array = false)
Then maybe check if $array is not true, and set all the $query[0] to NULL? So something like...
if(!$array) {
$query[0]->name = $query[0]->description = $query[0]->status = null;
}
There might be a more direct approach, but that's one way.
Firstly, if you're sending your query to the helper function as an array, there is no reason to turn it into another array. ie $array = array('$query') should just be $query this way you can access properties like this: $query->name as opposed to $query[0]->name. Secondly, if you're not editing the form entry, your $query would be empty, so you can use that as the trigger for what to return (either the blank form or the populated form):
function WorkOrderForm($query)
{
if($query!='')
{
//$formHTML=populated form
} else {
//$formHTML=empty form
}
return $formHTML;
}
Okay? But, there's a problem... The code you have in your helper won't work. You're using an arithmetic operator += to (assuming) concatenate the form data. What this does is try to add 1 to a string, which will always equal 0. What you're looking for is the .= operator; this will concatenate the form as would be expected. However, this offers you little control over how the form will look (as is, it will put all the form elements side-by-side -- not too pretty). What you could do is instead of concatenating them all together, push them into an array, then echo the form elements out one-by-one:
if($query!=''){
$form_array=array();
array_push($form_array,form_open('order/update',$attributes));
array_push($form_array,form_input('name',$query->name));
array_push($form_array,form_textarea('name',$query->description));
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
array_push($form_array,form_dropdown('status',$dropOptions,$query->status));
array_push($form_array,form_input('name',$query->startDate));
array_push($form_array,form_input('name',$query->endDate));
array_push($form_array,form_close());
}else{
$form_array=array();
array_push($form_array,form_open('order/add',$attributes));
array_push($form_array,form_open('order/update'));
array_push($form_array,form_input('name'));
array_push($form_array,form_textarea('name'));
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
array_push($form_array,form_dropdown('status',$dropOptions));
array_push($form_array,form_input('name'));
array_push($form_array,form_input('name'));
array_push($form_array,form_close());
}
return $form_array;
Then to present the form elements, iterate through the $form_array array that was returned:
$form_data='';//blank for new item, or data to populate form with to edit an item
$form_el = WorkOrderForm($form_data);
foreach($form_el as $key=>$val){
echo $val.'<br>';//I just added a <br> here so each form element will be on a new line; change to fit your needs
}
Hope this helps...

Drupal - Webform element theming

Another question about Drupal webforms --
The form itself is built in by /includes/form.inc's
function theme_form_element($element, $value)
and adds a <label> element to the $output. I want to remove that label only for one webform, so I have to override the function.
How can I override it for only one webform, while leaving it the same in all others?
E.g.
if ($block == 'contact'):
// only output <input> form element stored in $value
function mytheme_html_form_element($element, $value) {
$t = get_t();
$output .= " $value\n";
return $output;
}
endif;
Is this possible, and what goes in the if condition?
If you're just looking to remove the label, you can also use hook_form_alter(), and check that $form_id is equal to the webform in question. The id will be of the form: webform_client_form_N where N is the node ID of the webform.
Once you're operating on the proper form, you can unset the label using, for example, code like this:
unset($form['submitted']['first_name']['#title']);
Which would unset the label for a field called first_name.
i did have to do a hook_form_alter, but the label itself was in the ['submitted'] element.
here is the code
if($form_id == 'webform_client_form_18') {
$form['submitted']['#children'] = '
<input
type="text"
maxlength="128"
name="submitted[email]"
id="edit-submitted-email"
value="' . $form['submitted']['email']['#default_value']. '"
class="form-text required"
/>
';
}
in a different form, removing the #title worked (+1 for you!), but this was a different case.
I wouldn't unset form element titles. You could get unexpected results when your form gets rendered by the theme engine.
You can do it several ways:
Theme each element or the whole form with with '#theme' => 'my_callback'.
You can also create your own form element using hook_elements that uses a corresponding theme hook.
See:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html
http://api.drupal.org/api/function/hook_elements/6

How to extract value from hidden field on form

I have form (on my own blog/cms install which i want to play with a bit) with hidden value which i want to extract. Problem is that there are 2 forms on that page, each with that hidden field with value. On each form field name is the same, only hidden value differs. Something like this:
<input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>
Both look the same in html source. So, to help myself i opened php file with this page, edited it and added some random words before field that i need. So now one field (the one that i don't want) is like in above code but field i need is like this:
mywordshere <input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>
How do i extract value from field i need (with mywordshere before its code) if i have my page's html source in php variable (grabbed with libcurl)?
An example using DOMDocument
<?php
$html = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere">
</body>
</html>
HTML;
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->loadHTML( $html );
$node = $doc->getElementById( '_hiddenname' );
echo $node->getAttribute( 'value' );
?>
Note: your HTML string must have a DOCTYPE defined for this to work.
Assumably the two forms have different names, correct? So if you parse your scraped text with something DOM aware, you should be able to choose your input field by searching for it in its parent form.
The fact that you have two input fields named the same, and with the same id, is the real problem. The id attribute for HTML elements is supposed to be unique on a given page, and if it was, you could do this easily with a DOM parser. Example:
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$inputs = $dom->getElementsByTagName('input');
foreach ($inputs as $i)
{
if ($i->getAttribute('id') == 'targetId') {
//do some stuff
}
}
Since you can't take that approach, and you've marked your input with a string that you can identify, I would use a combination of string functions:
$str = 'mywordshere <input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>';
$pos = strpos($str,'mywordshere');
if ($pos !== false) {
$valuePos = strpos($str,'value=',$pos);
if ($valuePos !== false) {
//get text starting from the 'value=' portion of the string
$str = substr($str,$valuePos);
$arr = explode('"',$str);
//value will be in $arr[1]
echo $arr[1];
}
}
I would strongly recommend you re-work your element IDs however, and use the DOM approach.
The value will be available in either $_GET["_hiddenname"] or $_POST["_hiddenname"], depending on which method you are using. Which one you get will depend on which form is doing the submitting.
If you have two fields which are named the same within the same form, you have a bigger problem.

Finding name attributes inside a tag using php

i want to find the name attribute with in a tag in a form using php4 . Does any one help me how it find..
eg
<input type="text" name="txt_name" value="" >
I want to know name of all the fields
You need a library/code which implements an HTML DOM, look at these SO questions for more information.
When you submit a form, a url is send to the server in the form "name=value&name1=value1" etc. Here the "name" stands for the attribute "name" of an html element. So If you are using the $_GET or a $_POST then you can get the form item names in an array as follows
$names=array();
foreach($_GET as $key => $value)
{
$names[] = $key;
}
print_r($names);
You can use simple html dom as mentioned by Ignacio Vazquez-Abrams
Add it in your code like this
require_once('Simple_html_dom.php');
To use it I initialized it using this line
$html = new Simple_html_dom();
Load a page or link using the line
$html->load_file('http://www.yourste.com/yourpage.php');
to parse and find this element
$e = $html->find("input", 0);
the echo the value of attribute name
echo $value = $e->name

Categories