Content(context)-related block in Drupal7 - drupal

The case is following: I have a custom Content Type with a filed "data_field", for example. I want to have a block on a sidebar that will properly process and display data of this "data_field" field. How can I access that with Drupal API?
P.S. I understand that this problem can be solved with Views in some way, it has an ability of content-specific displaying, but I'm interested in the way it's achieved owing to API.

To render a field with the default Drupal settings for the content type:
$n = node_load($nid_of_content);
$n = node_view($n);
print render($n['field_somevalue']);
You can specify a node display format as well, eg
$n = node_view($n, 'teaser');
If you want to manipulate field content in a different way, use the raw field value:
$n = node_load($nid_of_content);
$value_you_want = $n->field_data_field_somevalue['und'][0]['value'];
This assumes you're not using any language/translation functionality - the 'und' stands for "undefined" and states that no language is has been explicitly set for the node.
If the field can contain multiple values, iterate through ['und'][0]['value'] to ['und'][x]['value']
The above code displays a raw value for most field types. Use print_r($n->field_somevalue) during development to find out what values are available to you; it depends on the field type. For example, file types give you 'uri' (among others) instead of 'value', text fields also have 'safe_value' which has input format filtering applied, long text fields will have 'summary' and 'safe_summary', &c.

Related

getting textfield output partly unvisible

In a Drupal content type a need to get the output of a field partly unvisible. These are bank account details, the IBAN.
Normally the field shows 1234567. I need to get xxxx567. I need to show only the last 3 numbers/letters.
Also I need this output in field edit form.
On the display end you could change the output using a simple PHP function in the theme template by grabbing a substring of the field's last three digits and concatenating it with "xxxx" before printing.
You might also consider doing this at the formatter level by using the 'custom formatter' module perhaps?
https://drupal.org/project/custom_formatters
To do this on the edit screen is trickier. I suppose you could do a hook form alter to use PHP to change the field value, but I am afraid you will rewrite the field value when you save the node with the 'xxxx' instead of the real data.
I wonder if it would make sense to 1.) hide the actual field, 2.) create a dummy field that displays the text formatted as "xxxx567" to the user, and 3.) write some javascript that populates the hidden field with the visible field's value if it is changed. Presumably the form would still throw values if the hidden field did not meet formatting requirements.

access fields from a dexterity behavior on a content type invariant check

I have a behavior that defines two fields: year and week (of the year).
This behavior is reused for several content types, and only in one of them I need to make sure that this fields are not repeated in any other instance of the same content type, i.e. two objects of this content type can not share the same year and week (is fine to share the same year or the same week).
As this restriction is only meant for this specific content type I tried with an zope.interface.invariant but for some reason I can not get access to the fields defined in the behavior.
A simplified version of the Content type would be:
class IMyContentType(form.Schema)
title = schema.TextLine(title="My title",
description="My description",
required=True,
)
#invariant
def check_year_and_week(data):
data.week
How can I get the value (if any) from within check_year_and_week invariant?
You can't. Invariants have access to values for other fields in the same interface, but not fields from other interfaces.
You can use a widget manager validator instead: http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#widget-manager-validators
Or do the validation in your form's action handler: http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers
A behavior is nothing but an adapter; if you're not getting the fields on the invariant you probably need adapt your content type before trying to access the extra fields.

Drupal views 3 - missing "MULTIPLE FIELD SETTINGS" in entity reference

Context: Content type Person has reference (multiple values) to a content type Work, using entity reference.
Need: To display the title of each person node which references a given work, separated by a comma.
Done: A view with a back reference, the right nodes are fetched. (Views 7.x-3.7)
Problem: Cannot display the value separated by a comma. Note: I usually do it with the "Simple separator" display type which is under "Display all values in the same row" in the MULTIPLE FIELD SETTINGS field group. However, this field group is not available in my context.
Solved
I have found the module Views Merge Rows - works very nice. If it does not support Features module for some reason, I can take some of its code code in order to use hook_views_pre_render myself.
I was able to work around this problem by using token_formatters. The basic steps (after token formatters is installed):
No relationship to referenced entity in views (not needed)
Add the entity reference field to the view
Change formatter to "tokenized text"
For 'Text to output' use a token (I'm using [node:field-name])
For 'link destination' use a token ('m using [entity:url:path] for a relative link)
Set multiple field setting as desired
You need a custom views Format here because you are talking about the whole views-row not a multiple results field. You can use the "Unformatted list" and add a comma to be added with CSS or JS.
What kind of Relationship do you use? Can you export your whole views in an external editor and provide a link?
I had a similar issue, where I was using the Entity Reference relationship of "Referencing Entity" instead of "Referenced Entity". (The reference was on the child and the View started at the parent level).
When you run a Drupal System Message on the row (dsm), it returns all the nid responses appropriately, but as different result rows instead of as a single object; however, since the NID field (like many others) has no option for display multiple results, it would only grab the first result.
I ended up having to do an Entity Query from a Views PHP field with the current row's NID as one of the Field Conditions. That seemed to do the trick, rather than trying to load a View inside of a View with views_field_view.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', '[your_content_type]')
->propertyCondition('status', 1)
->fieldCondition('[your_field_machine_name]', '[field_column_to_check]', $row->nid)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
I had a very similar problem: no "Multiple Field Settings" were available in the field configuration of a multi-value entity reference from my content type to User.
Solved it by removing the entity reference and instead using the multi-value "User ID" field of my content type directly. The "Multiple Field Settings" form area was available now and I selected "Display all values in the same row" there as you do normally. Now this would only display numeric user IDs separated by comma (not desired). But in the field configuration there was also a setting "Format:", which I set to "Label". This would display user names instead.
So I guess by creating a custom formatter you would be able to display your associated "Work" entities in a similar way.

how to change a drupal views output

I need to create a block with eg the latest comments on the site.
when using views, concegui select the data I wanted, but the problem is that I need to edit the output of view (specify the html). I tried to make a tpl, but the fields when they get to this, are already formatted ([#markup]). also tried to make a block programmatically by accessing the fields of view, via $comments = views_get_view('last_opinions');, but so the fields do not bring content, but for example, ids (for referrals), or integers (in the case of dates), ....
basically, how to change views output?
views->your view->advance(third panel)-> Theme: Information -> click Information ->choose tpl as per your requirement and find $row which actually prints your output

Drupal / CKEditor and input filtering, how to return clean value?

I have a Drupal set up with CKEditor and CCK, with a custom node which has some additional fields using CKEditor, one field being 'related_news'.
I'm trying to figure out the best practice for returning that data safely. What I have right now is:
$node = noad_load(35);
<h3>Related News</h3>
<?php print $node->field_related_news['0']['value']; ?>
But that returns potentially dangerous data, since no filtering is being used on it.
I've gone to my Input Settings and enabled Filtered HTML and I'm still getting this issue.
Any ideas? Best practices?
First of all, when you create the CCK field, there is a setting (under Global Settings)
Text processing:
[ ] Plain text
[x] Filtered text (user selects input format)
I hope you selected that the Filtered text setting. Only then it is input filtering done for that field.
The filtered string should be available as
$node->field_related_news['0']['safe']

Resources