Hi,
i want to generate a custom button, but i do not find a solution for my problem.
So i need a field with to values:

-label =>label
-value=>value

with these to parameters i can generate my button like:

label

the onclick-function is only the understanding my problem.

maybe someone can help me or knows a solution thanks a lot

Comments

AndyLicht’s picture

I think i am only a few meters for the finish.
I am using the field collection and the template of the field collection.

So that is the original code of the template:

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <div class="content"<?php print $content_attributes; ?>>
    <?php
//      print render($content); is the original
//these two statements give me the content i want to use
      print render($content['field_label']);
      print render($content['field_service']);
    ?>
  </div>
</div>

That is the code i am actually using:

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <div class="content"<?php print $content_attributes; ?>>
    <?php
        print render($content['field_beschriftung']);
        print render($content['field_dienst']);
        $label = render($content['field_beschriftung']);
        $value = render($content['field_dienst']);
        echo "<button type='button' value='" . $value . "' />" . $label . "</button>";
    ?>
  </div>
</div>

The Problem is that the code is not really clean i am getting a lot of divs, thats look like:

<button type='button' value='<div class="field field-name-field-dienst field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Dienst</div></div></div>' /><div class="field field-name-field-beschriftung field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Hinzufügen</div></div></div></button>

Waht can i do to get a clean code?

Thanks for helping me
Andy

cfox612’s picture

For code questions you would have better luck moving this to the module development forum.

Tobias Xy’s picture

You shouldn't use the render function when creating the value / label for the button.
I guess $content is an entity object or something like that?

If that's the case, you could access the raw values like this:

$label = $content['field_beschriftung'][LANGUAGE_NONE][0]['value'];
$value = $content['field_dienst'][LANGUAGE_NONE][0]['value'];

or

$wrapper = entity_metadata_wrapper('xxx', $content);
$label = $wrapper->field_beschriftung->value();
$value = $wrapper->field_dienst->value();
AndyLicht’s picture

Hi thanks for helping me.

Both variants give me an empty button. So i am getting:
<button type='button' value='' /></button>

by the first methode i get these error:
Notice: Undefined index: und in include() (Zeile 36 von C:\xampp\htdocs\geoportal_drupal_test\sites\all\modules\field_collection\field-collection-item.tpl.php).
Notice: Undefined index: und in include() (Zeile 37 von C:\xampp\htdocs\geoportal_drupal_test\sites\all\modules\field_collection\field-collection-item.tpl.php).

thanks for helping me.

Tobias Xy’s picture

Okay, could you then post the contents of $content? (var_dump($content); )

AndyLicht’s picture

Hi,
the var_dump promts me the following output:array(2)
{
["field_label"]=> array(18)
{
["#theme"]=> string(5) "field"
["#weight"]=> string(1) "0"
["#title"]=> string(5) "Label"
["#access"]=> bool(true)
["#label_display"]=> string(6) "hidden"
["#view_mode"]=> string(4) "full"
["#language"]=> string(3) "und"
["#field_name"]=> string(11) "field_label"
["#field_type"]=> string(4) "text"
["#field_translatable"]=> string(1) "0"
["#entity_type"]=> string(21) "field_collection_item"
["#bundle"]=> string(12) "field_button"
["#object"]=> object(FieldCollectionItemEntity)#56 (21)
{
["fieldInfo":protected]=> NULL
["hostEntity":protected]=> object(stdClass)#58 (29)
{
["vid"]=> string(1) "4"
["uid"]=> string(1) "1"
["title"]=> string(8) "WMS Test"
["log"]=> string(0) ""
["status"]=> string(1) "1"
["comment"]=> string(1) "2"
["promote"]=> string(1) "1"
["sticky"]=> string(1) "0"
["nid"]=> string(1) "4"
["type"]=> string(3) "wms"
["language"]=> string(2) "de"
["created"]=> string(10) "1376022424"
["changed"]=> string(10) "1376022424"
["tnid"]=> string(1) "0"
["translate"]=> string(1) "0"
["revision_timestamp"]=> string(10) "1376022424"
["revision_uid"]=> string(1) "1"
["field_button"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(2)
{
["value"]=> string(1) "1"
["revision_id"]=> string(1) "1"
}
}
}
["body"]=> array(0)
{
}
["rdf_mapping"]=> array(9)
{
["rdftype"]=> array(2)
{
[0]=> string(9) "sioc:Item"
[1]=> string(13) "foaf:Document"
}
["title"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(8) "dc:title"
}
}
["created"]=> array(3)
{
["predicates"]=> array(2)
{
[0]=> string(7) "dc:date"
[1]=> string(10) "dc:created"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
["changed"]=> array(3)
{
["predicates"]=> array(1)
{
[0]=> string(11) "dc:modified"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
["body"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(15) "content:encoded"
}
}
["uid"]=> array(2)
{
["predicates"]=> array(1)
{
[0]=> string(16) "sioc:has_creator"
}
["type"]=> string(3) "rel"
}
["name"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(9) "foaf:name"
}
}
["comment_count"]=> array(2)
{
["predicates"]=> array(1)
{
[0]=> string(16) "sioc:num_replies"
}
["datatype"]=> string(11) "xsd:integer"
}
["last_activity"]=> array(3)
{
["predicates"]=> array(1)
{
[0]=> string(23) "sioc:last_activity_date"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
}
["cid"]=> string(1) "0"
["last_comment_timestamp"]=> string(10) "1376022424"
["last_comment_name"]=> NULL ["last_comment_uid"]=> string(1) "1"
["comment_count"]=> string(1) "0"
["name"]=> string(11) "balschmiter"
["picture"]=> string(1) "0"
["data"]=> string(4) "b:0;"
["entity_view_prepared"]=> bool(true)
}
["hostEntityId":protected]=> int(4)
["hostEntityRevisionId":protected]=> bool(false)
["hostEntityType":protected]=> string(4) "node"
["langcode":protected]=> string(3) "und"
["item_id"]=> string(1) "1"
["revision_id"]=> string(1) "1"
["field_name"]=> string(12) "field_button"
["default_revision"]=> bool(true)
["archived"]=> string(1) "0"
["entityType":protected]=> string(21) "field_collection_item"
["entityInfo":protected]=> array(21)
{
["label"]=> string(21) "Field collection item"
["label callback"]=> string(18) "entity_class_label"
["uri callback"]=> string(16) "entity_class_uri"
["entity class"]=> string(25) "FieldCollectionItemEntity"
["controller class"]=> string(19) "EntityAPIController"
["base table"]=> string(21) "field_collection_item"
["revision table"]=> string(30) "field_collection_item_revision"
["fieldable"]=> bool(true)
["redirect"]=> bool(false)
["entity keys"]=> array(3)
{
["id"]=> string(7) "item_id"
["revision"]=> string(11) "revision_id"
["bundle"]=> string(10) "field_name"
}
["module"]=> string(16) "field_collection"
["view modes"]=> array(1)
{
["full"]=> array(2)
{
["label"]=> string(21) "Vollständiger-Inhalt"
["custom settings"]=> bool(false)
}
}
["access callback"]=> string(28) "field_collection_item_access"
["metadata controller class"]=> string(37) "FieldCollectionItemMetadataController"
["bundles"]=> array(1)
{
["field_button"]=> array(3)
{
["label"]=> string(29) "Field collection field_button"
["admin"]=> array(4)
{
["path"]=> string(62) "admin/structure/field-collections/%field_collection_field_name"
["real path"]=> string(46) "admin/structure/field-collections/field-button"
["bundle argument"]=> int(3)
["access arguments"]=> array(1)
{
[0]=> string(28) "administer field collections"
}
}
["rdf_mapping"]=> array(0)
{
}
}
}
["static cache"]=> bool(true)
["field cache"]=> bool(true)
["load hook"]=> string(26) "field_collection_item_load"
["translation"]=> array(0)
{
}
["schema_fields_sql"]=> array(2)
{
["base table"]=> array(4)
{
[0]=> string(7) "item_id"
[1]=> string(11) "revision_id"
[2]=> string(10) "field_name"
[3]=> string(8) "archived"
}
["revision table"]=> array(2)
{
[0]=> string(11) "revision_id"
[1]=> string(7) "item_id"
}
}
["configuration"]=> bool(false)
}
["idKey":protected]=> string(7) "item_id"
["nameKey":protected]=> string(7) "item_id"
["statusKey":protected]=> string(6) "status"
["defaultLabel":protected]=> bool(false)
["field_value"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(195) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
["format"]=> NULL
["safe_value"]=> string(231) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
}
}
}
["field_label"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(11) "Hinzufügen"
["format"]=> NULL
["safe_value"]=> string(11) "Hinzufügen"
}
}
}
["rdf_mapping"]=> array(0)
{
}
["entity_view_prepared"]=> bool(true)
}
["#items"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(11) "Hinzufügen"
["format"]=> NULL
["safe_value"]=> string(11) "Hinzufügen"
}
}
["#formatter"]=> string(12) "text_default"
[0]=> array(1)
{
["#markup"]=> string(11) "Hinzufügen"
}
["#printed"]=> bool(true)
["#children"]=> string(160) "Hinzufügen"
}
["field_value"]=> array(18)
{
["#theme"]=> string(5) "field"
["#weight"]=> string(1) "1"
["#title"]=> string(5) "Value"
["#access"]=> bool(true)
["#label_display"]=> string(6) "hidden"
["#view_mode"]=> string(4) "full"
["#language"]=> string(3) "und"
["#field_name"]=> string(11) "field_value"
["#field_type"]=> string(4) "text"
["#field_translatable"]=> string(1) "0"
["#entity_type"]=> string(21) "field_collection_item"
["#bundle"]=> string(12) "field_button"
["#object"]=> object(FieldCollectionItemEntity)#56 (21)
{
["fieldInfo":protected]=> NULL
["hostEntity":protected]=> object(stdClass)#58 (29)
{
["vid"]=> string(1) "4"
["uid"]=> string(1) "1"
["title"]=> string(8) "WMS Test"
["log"]=> string(0) ""
["status"]=> string(1) "1"
["comment"]=> string(1) "2"
["promote"]=> string(1) "1"
["sticky"]=> string(1) "0"
["nid"]=> string(1) "4"
["type"]=> string(3) "wms"
["language"]=> string(2) "de"
["created"]=> string(10) "1376022424"
["changed"]=> string(10) "1376022424"
["tnid"]=> string(1) "0"
["translate"]=> string(1) "0"
["revision_timestamp"]=> string(10) "1376022424"
["revision_uid"]=> string(1) "1"
["field_button"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(2)
{
["value"]=> string(1) "1"
["revision_id"]=> string(1) "1"
}
}
}
["body"]=> array(0)
{
}
["rdf_mapping"]=> array(9)
{
["rdftype"]=> array(2)
{
[0]=> string(9) "sioc:Item"
[1]=> string(13) "foaf:Document"
}
["title"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(8) "dc:title"
}
}
["created"]=> array(3)
{
["predicates"]=> array(2)
{
[0]=> string(7) "dc:date"
[1]=> string(10) "dc:created"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
["changed"]=> array(3)
{
["predicates"]=> array(1)
{
[0]=> string(11) "dc:modified"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
["body"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(15) "content:encoded"
}
}
["uid"]=> array(2)
{
["predicates"]=> array(1)
{
[0]=> string(16) "sioc:has_creator"
}
["type"]=> string(3) "rel"
}
["name"]=> array(1)
{
["predicates"]=> array(1)
{
[0]=> string(9) "foaf:name"
}
}
["comment_count"]=> array(2)
{
["predicates"]=> array(1)
{
[0]=> string(16) "sioc:num_replies"
}
["datatype"]=> string(11) "xsd:integer"
}
["last_activity"]=> array(3)
{
["predicates"]=> array(1)
{
[0]=> string(23) "sioc:last_activity_date"
}
["datatype"]=> string(12) "xsd:dateTime"
["callback"]=> string(12) "date_iso8601"
}
}
["cid"]=> string(1) "0"
["last_comment_timestamp"]=> string(10) "1376022424"
["last_comment_name"]=> NULL
["last_comment_uid"]=> string(1) "1"
["comment_count"]=> string(1) "0"
["name"]=> string(11) "balschmiter"
["picture"]=> string(1) "0"
["data"]=> string(4) "b:0;"
["entity_view_prepared"]=> bool(true)
}
["hostEntityId":protected]=> int(4)
["hostEntityRevisionId":protected]=> bool(false)
["hostEntityType":protected]=> string(4) "node"
["langcode":protected]=> string(3) "und"
["item_id"]=> string(1) "1"
["revision_id"]=> string(1) "1"
["field_name"]=> string(12) "field_button"
["default_revision"]=> bool(true)
["archived"]=> string(1) "0"
["entityType":protected]=> string(21) "field_collection_item"
["entityInfo":protected]=> array(21)
{
["label"]=> string(21) "Field collection item"
["label callback"]=> string(18) "entity_class_label"
["uri callback"]=> string(16) "entity_class_uri"
["entity class"]=> string(25) "FieldCollectionItemEntity"
["controller class"]=> string(19) "EntityAPIController"
["base table"]=> string(21) "field_collection_item"
["revision table"]=> string(30) "field_collection_item_revision"
["fieldable"]=> bool(true)
["redirect"]=> bool(false)
["entity keys"]=> array(3)
{
["id"]=> string(7) "item_id"
["revision"]=> string(11) "revision_id"
["bundle"]=> string(10) "field_name"
}
["module"]=> string(16) "field_collection"
["view modes"]=> array(1)
{
["full"]=> array(2)
{
["label"]=> string(21) "Vollständiger-Inhalt"
["custom settings"]=> bool(false)
}
}
["access callback"]=> string(28) "field_collection_item_access"
["metadata controller class"]=> string(37) "FieldCollectionItemMetadataController"
["bundles"]=> array(1)
{
["field_button"]=> array(3)
{
["label"]=> string(29) "Field collection field_button"
["admin"]=> array(4)
{
["path"]=> string(62) "admin/structure/field-collections/%field_collection_field_name"
["real path"]=> string(46) "admin/structure/field-collections/field-button"
["bundle argument"]=> int(3)
["access arguments"]=> array(1)
{
[0]=> string(28) "administer field collections"
}
}
["rdf_mapping"]=> array(0)
{
}
}
}
["static cache"]=> bool(true)
["field cache"]=> bool(true)
["load hook"]=> string(26) "field_collection_item_load"
["translation"]=> array(0)
{
}
["schema_fields_sql"]=> array(2)
{
["base table"]=> array(4)
{
[0]=> string(7) "item_id"
[1]=> string(11) "revision_id"
[2]=> string(10) "field_name"
[3]=> string(8) "archived"
}
["revision table"]=> array(2)
{
[0]=> string(11) "revision_id"
[1]=> string(7) "item_id"
}
}
["configuration"]=> bool(false)
}
["idKey":protected]=> string(7) "item_id"
["nameKey":protected]=> string(7) "item_id"
["statusKey":protected]=> string(6) "status"
["defaultLabel":protected]=> bool(false)
["field_value"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(195) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
["format"]=> NULL
["safe_value"]=> string(231) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
}
}
}
["field_label"]=> array(1)
{
["und"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(11) "Hinzufügen"
["format"]=> NULL
["safe_value"]=> string(11) "Hinzufügen"
}
}
}
["rdf_mapping"]=> array(0)
{
}
["entity_view_prepared"]=> bool(true)
}
["#items"]=> array(1)
{
[0]=> array(3)
{
["value"]=> string(195) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
["format"]=> NULL
["safe_value"]=> string(231) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
}
}
["#formatter"]=> string(12) "text_default"
[0]=> array(1)
{
["#markup"]=> string(231) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
}
["#printed"]=> bool(true)
["#children"]=> string(380) "http://mapserver.lgb-rlp.de/cgi-bin/wlf_gwe?SERVICE=WMS&VERSION=1.1.1&ST..."
}
}

jaypan’s picture

Please wrap your code in <code></code> tags.

So, what exactly is it that you are trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

redsd’s picture

Did you place your var_dump() at the same place you placed your code?
cause there is no reference to field_beschriftung and field_dienst.

can you just do a var_dump of $content['field_beschriftung'] and $content['field_dienst'];
like this:

echo "<pre>";
var_dump($content['field_beschriftung'], $content['field_dienst']);
echo "</pre>";
AndyLicht’s picture

I am sorry, i have changed it to field_label and field_value. Thanks for helping me.

And sorry, next time i will put it into the code-tags

Here is the solution:

$array = (array)$content['field_value']["#object"];
echo "<button class="" type='button' value='" . $array['field_value']['und'][0]['value'] . "' />" . $array['field_label']['und'][0]['value'] . "</button>";

The problem was, that the content-array contains an object, so you have to convert it, or use some other php functions. The solution shows the problem.
Thanks for helping me!!!!!