1

I've created a custom field in Display Suite using this code in my module:

function mymodule_ds_fields_info($entity_type) {
$fields = array();

$fields['node']['type_label'] = array(
  'title' => t('Label'),
  'field_type' => DS_FIELD_TYPE_FUNCTION,
  'function' => 'mymodule_ds_field_type_label',
);

function mymodule_ds_field_type_label($field) {

$entity = $field['entity'];
$content = $entity->type;

return $content;
}

Now, I was able to set the content of the field to match my content type, but I also want to set a dynamic class for this field equal to that content type, for example:

$fields['node']['type_label'] = array(
  'title' => t('Label'),
  'field_type' => DS_FIELD_TYPE_FUNCTION,
  'function' => 'mymodule_ds_field_type_label',
  'class' => 'CONTENTTYPE',
);

Or:

function mymodule_ds_field_type_label($field) {

  $entity = $field['entity'];
  $content = $entity->type;
  $class = $entity->type;

So the end result should be something like this:

<div class="field field--name-type-label field--type-ds CONTENTTYPE">...</div>

How can I do this?

Thanks in advance.

0

Each time Display Suite is used in content output, it's wrapped in an <article> element with the content type as a class. You can see below that the content type can be found as node-enterprise-blog class.

Display Suite Content Type Class

I'm not sure what your end purpose for this is, but this way you can select the element using .node-enterprise-blog .field-field-name. If you need to add a class to the field, consider using the Field Template option under DS Extras. It allows you to change the markup of each field in a view mode.

  • You're right, I didn't spot the node-FIELDNAME class the first time, thanks for pointing it out. If someoneis reading this, and wants a class on the field itself, you can do this by wrapping the content, like so: $content = '<div class="' . $entity->type . '">' . $entity->type . '</div>'; – Jeroen Jul 8 '13 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.