Extract URL from link field in Drupal? - php

I have a link field that is composed from a URL and the title, I need to print out only the URL of the link field without the title in my node content type tpl file, is that possible ?
Thanks!

It should be as easy as:
$url = $node->field_name_of_field[$node->language][0]['url'];
I'll break that down a bit:
Fields are members of the node object and are always prefixed with field_ so a field called my_field can be found with $node->field_my_field.
Each field members of the node object is itself an array of all different language versions for the field, keyed by the language key. To access the field value for the language that the node is denoted as you would use: $node->field_my_field[$node->language] or perhaps $node->field_my_field[LANGUAGE_NONE] (which is the default).
Further to that, each language array can potentially have multiple field values in it, if the cardinality of the field is greater than 1. If you have a field (e.g. images) with multiple values allowed you would run through each like this:
foreach ($node->field_my_field[$node->language] as $delta => $item) {
}
Within each item of the language array are the actual field values. Fields may have multiple columns (for example the link module has url, title and attributes). To continue with the previous example you would find the url and title like this:
$url = $node->field_name_of_field[$node->language][0]['url'];
$title = $node->field_name_of_field[$node->language][0]['title'];
Hope that helps!

Related

TBS (TinyButStrong) get last value

I have a Word template, where I go through a TBS block and dynamically display the values. Now I'd like to compare the actual value with the last value displayed. Is there any possibility to solve this in word?
I was thinking of setting a variable and save the last value in this variable. So I only have to compare my own variable with the actual value. But I cant figure out, whether this is possible or not. Any help or other suggestions?
Example
*[myblock;block=begin]
[myblock.entry] // here I want to check if its the same as the last entry
[myblock;block=end]*
TinyButStrong cannot do that in native.
But you can use parameter « ondata » and a PHP user function in order to add the previous value in the current record.
You can also use a method of an object (see TBS documentation)
PHP :
function f_ondata_user($BlockName, &$CurrRec, $RecNum) {
static $entry_prev = '';
$CurrRec['entry_prev'] = $entry_prev;
$entry_prev = $CurrRec['entry'];
}
Template :
*[myblock;block=begin;ondata=f_ondata_user]
[myblock.entry]
[myblock.entry_prev] // here I want to check if its the same as the last entry
[myblock;block=end]*

CakePHP form - remove month field name

I'm setting up a Stripe payment form, so I need to remove the names of my month and year fields so they aren't sent to my server. The following code though, still gives the field a name of '[month]' and if the text of the array's name variable were 'xyz', the field would be named 'xyz[month]'. How can I remove the entirety of the field's name?
echo $this->Form->month('expiration_month', array('name' => '', 'data-stripe' => 'exp_month', 'default' => 'January'));
According to the documentation, the name of the <select> element is derived from the first function argument ("expiration_month" in your example.) If you take a look at the code, you can see the value "month" is hard-coded.
The only way around this is to manually build your own <select> element, or just ignore the value when it comes to your server. But why make users fill out a form element that isn't going to be processed by your server?
So, the quick and dirty way around this is to find the select field with js and overwrite the name attribute.

form_multiselect gives only one option

Sorry, but this is kind a homework question...
I have to make a webpage with codeigniter and I have to use multiple select component.
So my code.
Part in *view.php file:
<br>Keywords:<br>
<?php echo form_multiselect('keywords', $keys); ?>
Also there is submit button, and after it pressed I take POST data. For debugging tried:
var_dump($_POST['keywords']);
This always shows, that there is only one option selected, for example, string(1) "2"
Can someone advice how should I modify my code to get all selected items.
Please try:
<?php echo form_multiselect('keywords[]', $keys); ?>
A multiselect form field must have a name with array notation.
You would expect codeignitors function to accommodate this, but it doesnt (well not when i last used CI in 2010)
From the Codeigniter documentation:
form_multiselect()
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using form_dropdown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
The last sentence states you need to use POST array syntax, so the name of the select should be, in your case
name="keywords[]"

Assign array value to a field

I am working with migration and I am migrating taxonomy terms that the document has been tagged with. The terms are in the document are separated by commas. so far I have managed to separate each term and place it into an array like so:
public function prepareRow($row) {
$terms = explode(",", $row->np_tax_terms);
foreach ($terms as $key => $value) {
$terms[$key] = trim($value);
}
var_dump($terms);
exit;
}
This gives me the following result when I dump it in the terminal:
array(2) {
[0]=>
string(7) "Smoking"
[1]=>
string(23) "Not Smoking"
}
Now I have two fields field_one and field_two and I want to place the value 0 of the array into field_one and value 1 into field_two
e.g
field_one=[0]$terms;
I know this isn't correct and I'm not sure how to do this part. Any suggestions on how to do this please?
If you are only looking to store the string value of the taxonomy term into a different field of a node, then the following code should do the trick:
$node->field_one['und'][0]['value'] = $terms[0];
$node->field_two['und'][0]['value'] = $terms[1];
node_save($node);
Note you will need to load the node first, if you need help with that, comment here and will update my answer.
You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So an ArrayList implements the List interface and a HashMap implements the Map interface.
List:
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Map:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
So as other answers have discussed, the list interface (ArrayList) is an ordered collection of objects that you access using an index, much like an array (well in the case of ArrayList, as the name suggests, it is just an array in the background, but a lot of the details of dealing with the array are handled for you). You would use an ArrayList when you want to keep things in sorted order (the order they are added, or indeed the position within the list that you specify when you add the object).
A Map on the other hand takes one object and uses that as a key (index) to another object (the value). So lets say you have objects which have unique IDs, and you know you are going to want to access these objects by ID at some point, the Map will make this very easy on you (and quicker/more efficient). The HashMap implementation uses the hash value of the key object to locate where it is stored, so there is no guarentee of the order of the values anymore.
You might like to try:
list($field_one, $field_two) = prepareRow($row);
The list function maps entries in an array (in order) to the variables passed by reference.
This is a little fragile, but should work so long as you know you'll have at least two items in your prepareRow result.

drupal---theme code

$i = 0;
$suggestion = 'page';
$suggestions = array();
while ($arg = arg($i++)) {
$arg = str_replace(array("/", "\\", "\0"), '', $arg);
$suggestions[] = $suggestion . '-' . $arg;
if (!is_numeric($arg)) {
$suggestion .= '-' . $arg;
}
}
i am a newbie of drupal,i can't follow the above code well, hope someone can explain it to me.i know the first line is assign the 0 to $i,then assign 'page' to an array. and i know arg is an array in drupal.
for example, now the url is example.com/node/1. how to use this url to use the above code.
It looks like its purpose is to generate ID strings (probably for a CSS class) depending on paths and excludes numeric components of the path out of the generated ID.
For example, 'my/123/article' produces the ID "page-my-article".
It seems this comes from a function (because the loop reads parameters using arg()) and that it expects Drupal paths such as "node/123/edit".
So the function would be called something like that:
mystery_function("my/123/article", "even/better/article");
Variables:
$i is the variable that stores the loop index
$suggestion is a String that store the generated ID. It is initialized to "page" because the ID is meant to have the syntax "page-SOMETHING".
$arg comes from the while loop: it reads the parameters passed to the mystery function one by one
$suggestions is an array that contains the generated IDs, one per argument passed to the mystery function.
In the loop:
The "$arg = str_replace..." line removes unwanted characters like "\" (however that line could definitely be improved).
The "$suggestions[] = ..." line adds the ID to the array of results.
The "if (!is_numeric($arg)..." line excludes numbers from the generated ID (e.g. "my/123/article" is probably supposed to produce "my-article")
The "$suggestion .= ..." line appends the value of "$arg" to the value of "$suggestion" and stores it in "$suggestion"
But honestly, I wouldn't advise to use that code: I doubt it works as intended given $suggestion isn't reinitialized at each loop so the value of the first path will get attached to the second, and to the third, and so on, and I doubt that is intentional.
Most likely this code is located in a theme's template.php in the preprocess_page hook. If that is the case, it's used to create template suggestions based on an argument supplied like the node id, to make it possible to create a page template per node.
What this code does, is that it loops though all the arguments in the drupal url, This could be user/3, node/3, taxonomy/term/3 or any custom url.
It first does some cleanup in the argument, to make sure that no weird symbols is added. This is not needed for most urls, but is probably there as a safety to avoid needing to have to create weird template names in some cases. This is done with str_replace
Next it adds the a suggestion to the list, based on the arg.
If the arg isn't numeric it adds that to the suggestion so it will be used in the next loop.
The idea is that you with the above urls will get added some template suggestions that look like this:
page
page-user
page-user-3
And
page
page-taxonomy
page-taxonomy-term
page-taxonomy-term-3
In this list drupal will use the last one possible, so if page-user-3.tpl.php exists, that will be used as page template for user/3, if not page-user.tpl.php will be used and so on.
This can be desired if you want to create customize page templates for the users page, or the nodes page while being able to create customized page templates for specific users.
This is not, however, a strategy I would want to employ. If you do this, you will end of with lots of different versions of a page template, and it will end up creating the maintenance nightmare that CMS systems was supposed to eliminate. If you truly need this many different page templates, you should instead look at context or panels, and put some logic into this instead.

Categories