How to alter field value drupal 7 - drupal-7

I am working with drupal 7, and wanted to change the output of "number_float" value when it is "0.00". I have digged into field.api but has no clue what function to do.
To say it in plain English:
if the field type "number_float" and value is "0.00", print "empty value".
This is also to consider before views output.
Any hint or guidance would be very much appreciated.
Thanks
UPDATE:
I used hook_field_attach_view_alter. It does as expected, however I wonder if this is the right thing.
function mymodule_field_attach_view_alter(&$output, $context) {
foreach (element_children($output) as $field_name) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'number_float' && $element['#formatter'] == 'number_decimal') {
foreach ($element['#items'] as $delta => $item) {
if ($element[$delta]['#markup'] == '0.00' || $element[$delta]['#markup'] == '0,00') {
$element[$delta]['#markup'] = t('Empty value message');
}
}
}
}
}
Any suggestion or betterment will be the answer.
thanks

A more standard Drupal way would be to manipulate the value in a preprocessor function. You can use hook_preprocess_HOOK for a theme function or template that's defined by another module. Inside of it, test for the 0.00 value and replace.

The update with hook_field_attach_view_alter is the way to go since nobody provides any other suggestion.

Related

unexpected result in a query in laravel

I’m a beginner in Laravel but have a problem at first. I wrote this query and I’m waiting for Sonya Bins as result but unexpectedly I see ["Sonya Bins"]. what’s the problem?
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
return view('products',compact('articles'));
});
pluck will return array if you want to get only single value then use value
// will return array
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
//will return string
$articles=DB::table('users')->where('id','2')->value('name');
// output Sonya Bins
here is an example from the documentation:
if you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
Read more about it here
Hope it helps.
Thanks
pluck() used to return a String before Laravel 5.1, but now it returns an array.
The alternative for that behavior now is value()
Try this:
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->value('name');
return view('products',compact('articles'));
});
I think it's easier to use the Model + find function + value function.
Route::get('products', function () {
$articles = User::find(2)->value('name');
return view('products',compact('articles'));
});
pluck will return the collection.
I think id is your primary key.
You can just get the first record, and call its attribute's name:
DB::table('users')->where('id','2')->first()->name;
or
DB::table('users')->find(2)->name;
First thing is that you used invalid name for what you pass to view - you don't pass articles but user name.
Second thing is that you use get method to get results instead of first (or find) - you probably expect there is only single user with id = 2.
So to sum up you should use:
$userName = DB::table('users')->find(2)->name;
return view('products',compact('userName'));
Of course above code is for case when you are 100% sure there is user with id = 2 in database. If it might happen there won't be such user, you should use construction like this:
$userName = optional(DB::table('users')->find(2))->name;
($userName will be null if there is no such record)
or
$userName = optional(DB::table('users')->find(2))->name ?? 'No user';
in case you want to use custom string.

How to remove / filter parameter in Typewriter

I have the below script in Typewriter
$Methods()[
$name($Parameters[$name$IsNullable: $WriteType][, ]) {}
]
...which works fine.
Now I want to check whether my method in c# has a parameter named commandId and if so not add it to type script (removing it from $Parameters)
This is my method for checking if the parameter's name is commandId;
bool IsCommandIdParameter(Parameter p)
{
return (p.Name == "commandId" && p.Type.Name == "string");
}
I found answer and I put it here in case somebody else has same issue, you can filter parameters by lambda expressions
$Methods()[
$name($Parameters(p => !IsCommandIdParameter(p))[$name$IsNullable: $WriteType][, ]) {}
]

Ext JS: Template with period in dataIndex

I'm trying to create an XTemplate, and one of my dataIndexes has a period... so my data looks something like this:
{
"one": 1,
"two.one": 21,
"two.two": 22
}
Now, when I'm creating the template, I can't do something like {two.one} because that thinks two is the beginning of an object, and then I'm accessing its key one. I've tried doing something like {'two.one'}, but this also doesn't work, and I've tracked it down to the culprit in Ext.XTemplateCompiler.parseTag. This code is what breaks it:
// compound Javascript property name (e.g., "foo.bar")
else if (isNaN(name) && name.indexOf('-') == -1 && name.indexOf('.') != -1) {
v = "values." + name;
}
// number or a '-' in it or a single word (maybe a keyword): use array notation
// (http://jsperf.com/string-property-access/4)
else {
v = "values['" + name + "']";
}
So with my two.one string, I get into that else if, but what I really want is the else that follows right after it. Unfortunately, it doesn't seem like I can override this in an easy way... does anybody have any thoughts? I'm using Ext JS 4.2.1.
Thanks to skirtle over on the Sencha Forums, I was able to solve it:
Instead of using {two.one} or {'two.one'}, it should be {[values['two.one']]}. Using values directly was the ticket.

ColdFusion 8 ArrayFind Substitute

I have an array that has a structure of ImageID and Custnum.
I need to find a particular ImageID and retrieve the Custnum for it.
I’m using ColdFusion 8 which does not have an ArrayFind command.
How would I do this without looping through each item? Thanks.
Your question may be answered to a point here "Is there a function similar to ArrayFind from ColdFusion 9 in ColdFusion 8?" but I don't see any other way apart from looping.
You can always create and use an UDF but it would have to use looping.
Why exactly you don't want to use looping anyway? Do you have that many elements in the array? Just remember to use cfbreak after finding your element to stop going through the rest of the array.
Given your situation, I don't think arrayFind() would help you much anyhow, as to find something with arrayFind(), you need to know exactly what you're looking for anyhow. And whilst you know your ImageID, you don't know the Custnum associated with it (hence your underlying problem).
There's nothing native in CF which can help you here, but there's a UDF on CFLib - structFindKeyWithValue() which'll do the trick.
It's written for CF9, but is easily backported to CF8. The modified, CF8-friendly version - is in the example below:
<cfscript>
a = [
{ImageID=1, Custnum=1},
{ImageID=2, Custnum=2},
{ImageID=3, Custnum=3},
{ImageID=4, Custnum=4}
];
testStruct = {a=a};
result = structFindKeyWithValue(testStruct, "ImageID", 2, "ONE");
function structFindKeyWithValue(struct, key, value, scope){
var keyResult = false;
var valueResult = [];
var i = 0;
if (!isValid("regex", arguments.scope, "(?i)one|all")){
throw(type="InvalidArgumentException", message="Search scope #arguments.scope# must be ""one"" or ""all"".");
}
keyResult = structFindKey(struct, key, "all");
for (i=1; i <= arrayLen(keyResult); i++){
if (keyResult[i].value == value){
arrayAppend(valueResult, keyResult[i]);
if (scope == "one"){
break;
}
}
}
return valueResult;
}
</cfscript>
<cfdump var="#result#">
Note that because it's a stuct function, you need to wrap your data in a struct first. Other than that it's fairly straight fwd.

How to set nested record using "set()"?

I'm facing problem in setting my record value.
I have nested record inside store like this :
- data
act_reading // I can set value of this record using -> record.set('act_reading', 'dsds');
adj_reading
act_reading_nested
- data
arr_act_colour // How can I set this record?
arr_act_rating // How can I set this record?
arr_act_ferrous // How can I set this record?
idrectype1 // How can I set this record?
adj_reading_nested
- data
arr_adj_colour
arr_adj_rating
arr_adj_ferrous
idrectype2
How can I set idrectype1 value inside act_reading_nested?
I also have to do the same thing for arr_act_colour, arr_act_rating, & arr_act_ferrous.
Thanks in Advance
Assuming record is variable holding reference to your recrd, wouldn't that work?
record.get('act_reading_nested').set('arr_act_colour','value');
record.get('act_reading_nested').idrectype1 = 'something';
Yes it would. Just checked.
From my experience with current implementation of the Store you can not :( I'm facing with such kind issue too, when I'd like to edit a non-plain Store in the GridPanel. see http://www.sencha.com/forum/showthread.php?119573-Event-beforeedit-in-EditableGridPanel&highlight=afteredit
I did not check it myself (rather found a hack workaround), but more clean way is to fix it with your own Store implementation using of Ext.override(Ext.Store, { ...}) facility.
See how I did it for 'standard' HttpProxy implementation.
Ext.override (Ext.data.HttpProxy, {
buildUrl : function (action, record) {
var ret = '';
if (window.location.pathname != '/') {
ret = window.location.pathname;
};
return ret + Ext.data.HttpProxy.superclass.buildUrl.call(this, action, record);
}
});
It requires a bit deep Extjs internal knowledge though.

Resources