comma separated views list - drupal

everyone. I've got a problem with Views 2. I have a view with row's style set to fields (Got only title field). I want to display these titles with comma separated list.
For example:
Kazakhstan, England, China, Korea
tried to do this:
foreach($fields['title']->content as $titles) {
$zagolovki[] = $titles['view'];
}
$title_list = implode(', ', $zagolovki);
print $title_list;
but it doesn't works - says error in argument. Please help me someone to display node titles in views with comma separated list. Thanks!

I quikly took a look in the views-view-fields.tpl.php that comes with the views module and it says
/*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
* - $field->content: The output of the field.
* - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
* - $field->class: The safe class id to use.
* - $field->handler: The Views field handler object controlling this field. Do not use
* var_export to dump this object, as it can't handle the recursion.
* - $field->inline: Whether or not the field should be inline.
* - $field->inline_html: either div or span based on the above flag.
* - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*/
So I think $fields is what you should iterate over. If you want to debug the structure of your $fields install the devel-module and use dpm() or dsm() to display the content of $field. Maybe take the template you edited (that should be one of the view-module templates in the views/theme folder) and look what happens there.

Where does it say the error occurs, though? nonsenz is probably right that $fields['title']->content is not an array. For debugging only, try adding
print("array content: "+ is_array($fields['title']->content));
before the foreach. If you know that $fields is a small nested structure, you can try a
print(str_replace("\n","<br/>",print_r($fields,true));
to see what exactly is in it, so that you can make verify that what you're trying to iterate over is in fact iterable.

I've created this module:
http://drupal.org/project/views_delimited_list
I'm not too sure about fiddling with the options to create merely a comma-separated list. You should be able to, and if you can't I'll fix it.

Related

Drupal 9 Computed Fields Module

I installed the computed fields modules and I'm trying to make a computed field hook for the below.
But I am not sure whether I put the code in the right place.
I just added the code below to the existing compute_field_api.php that comes with the compute field module. Is this the correct place to put this hook?
It doesn't seem to work and it doesn't display.
function computed_field_field_rating_average_compute($entity_type_manager, $entity, $fields, $delta)
{
// Get rating fields to compute
$facilities_and_services = $entity->field_facilities_and_services->value;
$fairway_rating = $entity->field_fairway_rating->value;
$recommendable_to_friends_rating = $entity->field_recommendable_to_friends->value;
$food_rating = $entity->field_food_rating->value;
$value_rating = $entity->field_value_rating->value;
$english_rating = $entity->field_english_rating->value;
$layout_rating = $entity->field_layout_rating->value;
$quality_rating = $entity->field_quality_rating->value;
$greens_rating = $entity->field_greens_rating->value;
$length_rating = $entity->field_length_rating->value;
// Set Computed field value
$value = ($facilities_and_services + $fairway_rating + $recommendable_to_friends_rating + $food_rating + $value_rating + $english_rating + $layout_rating + $quality_rating + $greens_rating + $length_rating) / 10;
return $value;
}
I tried to clear all the caches after adding this code. But it doesn't seem to work.
Had the same issue. Had to do the following:
Installed the module per the readme instructions (follow those).
Create the computed field function as you have above inside an enabled module (you may need to create a custom module for that). Note that the name of your custom module should not precede the function name (so it shouldn't be mymodule_computed_field_field_rating_average_compute, but rather as you have it above - i.e., how you've written your function is fine).
Flushed all caches.
Edit and save the node(s) where the new value should appear. That causes it to create a row in the field table.
After that, the computed value should appear in the saved content, and in views for that resaved content.
Note that it doesn't seem to be dynamic (like the old dynamic fields in D6 used to be), so it won't just magically appear for all existing content. To achieve that en-mass, I had to do a bit of back-end SQL to avoid having to manually re-save every entity (didn't want to do that for over 120 of 'em). Still figuring out if I have to do some other trickery to keep the field up-to-date despite my cache settings, as the value I'm attempting to display is dynamic, and not dependent upon the node in which the field exists...

What is the standard way to get/set datetime from fluid in typo3 flow?

What is the standard way to get/set datetimes to/from fluid in typo3 flow?
Is there editable html field which - like a basic f:form.textfield - will allow the datetime to be entered, edited and persisted?
I know I can display a datetime with eg
<f:format.date format="d.m.Y - H:i:s">{productArticle.classdate}</f:format.date>
but in that case the value is null nothing gets displayed and no entry is possible anyway. Does property work with f:format.date in some version? I get "property not registered" when I try it.
Or do I need to have two fields on my page, one for date and one for time, that I put together in the Action?
Thanks
I'm not sure if it's standard way, and personally I don't like it.. but it can be done like this:
In Fluid for new/edit actions:
<f:form action="create" objectName="girlfriend">
...
<f:form.textfield property="birthDate" placeholder="1991-12-30 - 18:25:58" value="{newGirlfriend.birthDate->f:format.date(format:'Y-m-d - H:i:s')}" />
...
</f:form>
In your controller you can add initialize*Action and update propertyMappingConfiguration for \DateTime if standard isn't good for you (it has to be done for both create and update action):
public function initializeCreateAction() {
$mappingConfig = $this->arguments['girlfriend']->getPropertyMappingConfiguration();
$mappingConfig->forProperty('birthDate')->setTypeConverterOption(
'TYPO3\Flow\Property\TypeConverter\DateTimeConverter',
\TYPO3\Flow\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
'Y-m-d - H:i:s'
);
}
Sometimes it's easier to not pass object directly to controller but create it on service layer. You can also take a look at TypeConverters - they do not require initializeActions and you can easily override existing ones setting higher priority..

How can I always force most recent articles with the exact phrase using Drupal Apache Solr?

I want to find exact phrase results looking anywhere in the body or the title. It has to be ordered from the most recent to oldest. The only way I get pretty close results is by forcing quotes around all my searches via this hook:
/**
* Implements hook_apachesolr_query().
*/
function hcp_node_apachesolr_query_prepare($query) {
// Adding quotes to all searches so it gives an accurate search result
$search_result = $query->getParam('q');
$has_quotes = preg_match('/^(["\']).*\1$/m', $search_result);
if(!$has_quotes) {
$query->replaceParam('q', '"' . $search_result . '"');
}
}
My Bias Settings:
Results Biasing: All are set to ignore but More recently created is set to 10
Type Biasing: All set to ignore
Field Biasing: All set to omit.
This seems to work well with two or three word phrase but if for example I search a whole title of an article it can't find anything.
I tried setting the Title and The full, rendered content field bias to the same thing (21). Again it would show good results but it wouldn't be in most recent order(I think this was pretty close but just wish it displayed most recent first).
I also tried updating my schema.xml: updated WhitespaceTokenizerFactory to KeywordTokenizerFactory but I didn't get good results.
This is how I fixed this:
Installed Apache Solr Term Proximity Module Updated field bias to .3
for both Title and the full rendered content (the rest are set to
omit) Set the term proximity to .1
Set the Most recent created to 10
FYI I tried different settings and this seems like the best.

Theming view for multiple value field

I know this is probably a n00b question but I've searched everywhere for an answer and havent found anything.
I have a CCK multiple value field for "Features" where a product can have a random number of multiple features entered for it. I am editing the view so I can style the output of the features on the product page.
Right now in my view I can output the entire list of features at once using:
<?php print $fields['field_features_value']->content ?>
This will give me a list all the features given for a product. But what I want to do is loop through and pull out each individual feature and format/style it separately. How exactly would I do this?
I ran into this yesterday again, and spent hours trying to Google the syntax, to no avail.
I was able to get this to work, but I must admit it is not the best way. It is duplicating some of the work that Views has already potentially done for us, and should be considered a brute-force approach.
My use case involved theming each filefield file in a node separately, per node-based row:
<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
/**
* This template is used to print a single field in a view. It is not
* actually used in default Views, as this is registered as a theme
* function which has better performance. For single overrides, the
* template is perfectly okay.
*
* Variables available:
* - $view: The view object
* - $field: The field handler object that can process the input
* - $row: The raw SQL result that can be used
* - $output: The processed output that will normally be used.
*
* When fetching output from the $row, this construct should be used:
* $data = $row->{$field->field_alias}
*
* The above will guarantee that you'll always get the correct data,
* regardless of any changes in the aliasing that might happen if
* the view is modified.
*/
?>
<?php
$output = explode('|', $output); // I've rewritten the field output in Views like this: [field_portfolio_image-field_name]|[nid]
$paths = $output[0]; // I've set filefield to show file paths rather than the file
$nid = $output[1]; // The NID is all that's really needed for this approach
$node = node_load($nid);
$slots = $node->field_portfolio_image;
foreach($slots as $prop) {
print ''.$prop[data][description].'';
}
?>
I used the Devel module heavily here (image reference for this example attached), in order to get the nested values I needed.
I know there is a better, proper way of doing this, rather than reloading the node data, since views should already have access to this upon page load.
When theming views is too specific I set conditions, relationships, parameters and all the views stuff except fields. The only field I use is node id.
Then when doing the theming I use...
$node = node_load($nid);
... to get the node object. You can inspect the node object with the dpm function that comes with the devel module.
dpm($node);
This "technique" works well for nodes and when you don't care about optimization or speed, because if you are going to do this with 1000 nodes you should load the nodes on batch.

How to change the label of the default value (-Any-) of an exposed filter in Drupal Views?

I created a view which has three exposed filters. Everything works fine except the fact that I can neither translate or change the default string (-Any-) for the dropdowns. Is there a way to change this string to something more meaningful like "Please Select" and make it translatable so the German version displays "Bitte wählen"? I have two screen captures that may be helpful:
and
A further improvement would be the ability to change the text "any" to something like "please select a (field name here)" but I am losing hope for that =)
UPDATE
IMPORTANT: On further testing, I found that if you choose to display "-Any-" from "admin/build/views/tools", then THAT IS translatable.
For anyone who wants to just change the value of "- Any -" to something in particular then use a custom module to override that looks like this:
function yourmodulename_form_alter(&$form, $form_state, $form_id) {
if($form_state['view']->name == 'your_view_name_here') {
$form['your_dropdown_name']['#options']['All'] = t('- Type -'); // overrides <All> on the dropdown
}
}
The reason you might want to do this is if you have 3 (for example) dropdowns for 3 separate fields. Then having on them wouldn't be very useful for a user (especially if you are not using labels).
In the code above just remember to change "yourmodulename" to the name of your module.
your_view_name_here should be the name of your view (replace dashes with underscores - for example "property-search-bar" would become "property_search_bar")
And change "your_dropdown_name" to the field name - I found this by using dsm($form) with the devel module installed and enabled. This is usually the field name of your drop down so it might be something like "field_my_custom_value".
Hope this helps anyone who needs it!
Three options:
You could change it with localisation, if you have that enabled already. Introducing localisation only for this string is far too much overhead.
You can change it with a form_alter, if you already alter the form anyway. Introducing a module with a hook_form alter for just one string is way too much (maintainance and performance) overhead.
You coud change it with a simple string override in your settings.php
In Drupal 7 (Drupal6 differs in details only)
/**
* String overrides:
*
* To override specific strings on your site with or without enabling locale
* module, add an entry to this list. This functionality allows you to change
* a small number of your site's default English language interface strings.
*
* Remove the leading hash signs to enable.
*/
$conf['locale_custom_strings_en'][''] = array(
'<Any>' => 'Whatever!',
);
Note though, that this will change every occurrance of the full string <Any> (case sensitive) to Whatever, not just the ones in that single form.
Views exposed filter label is not translatable in D6.
Go to Administer > Site building > Views and select tab tools.
Replace 'Label for "Any" value on optional single-select exposed filters: ' by the translatable '- Any -'.
Important: visit the views with exposed filters in at least one language which isn't your default language.
Then you can translate "- Any -" through Aminister > Site building > Translate interface (case sensitive).
Or you can simply use a line of jQuery code like this:
$(document).ready(function(){
$("#views-exposed-form-url-name-display-name #edit-tid-all a").text("All");
});
The Better Exposed Filter module allows you to change the "-any-" label in a Views exposed filter.
I'd rather go with the simple solution: String Overrides.
With this you simply add a string you want to change on your site, and replace it with anything you want (Strings of course).
May be module https://www.drupal.org/project/views_advanced_labels helps?
I found it, but have not tried it yet.
if ($form['#id'] == 'views-exposed-form-project-search-block-project-search') {
foreach ($form['#info'] as $filter_info) {
$filter = $filter_info['value'];
if ($form[$filter]['#type'] == 'select') {
$form[$filter]['#options']['All'] = $filter_info['label'];
}
}
}
If you use Better Exposed Filters module, go into Exposed Form > Exposed form style: Better Exposed Filters | Settings > look for your field > Advanced Filter Options > put "- Any -|All" in the "Rewrite the text displayed" field.

Resources