Getting fid of attached file in Drupal 7 - drupal

I added an additional field with a type "File" to an "article" content type. Now I need to get a fid of the uploaded file in hook_node_view(). I use for this the next code:
$my_form['fid']['#value'] = $node->field_image_svg[$node->language][0]['fid'];
The problem is that when I check "Include file in display" before saving a node, I get a fid correctly. But when I do not check, I get a warning:
Notice: Undefined offset: 0 in testmodule_node_view()
What's the problem? Is there way to get a fid of an uploaded file not depending on whether the file is displayed or not?

You are using the Enable Display field setting for the file field:
When the "Include file in display" checkbox is checked, you allow it to be rendered in the view mode of the node and so your field gets added into the language array of the field_image_svg field:
$node->field_image_svg[$node->language][0]['fid']
If unchecked it will not be allowed to and the language array will not keep the file information:
$node->field_image_svg[$node->language]
So if you want to get the fid not depending on that checkbox and still use it as a setting, you are forced to make a check on wether it exists or not to get rid of the notice and instead get the fid with:
$fid = db_query('SELECT fid FROM {file_usage} WHERE id = :id',array(':id' => $node->nid))->fetchField();
Another solution would be to remove the checbox (in field settings) and manage it to be hidden or shown through Manage display. This will keep the file's information in hook_node_view.
Hope this helps.

Related

While updating .docx file using docx4j, Read only fields become editable

In my docx file we have some readonly fields but, While i'm updating docx file using docx4j library that read only fields become editable.
XML Block
In document we have some section and readonly block for guide the user and below that section we have added block for enter value.
Dependency
compile "org.docx4j:docx4j-JAXB-ReferenceImpl:8.3.4"
In Word, Review > Restrict Editing > Editing Restrictions > Filling in Forms
results in the following in the Document Settings part, word/settings.xml:
<w:documentProtection w:edit="forms" w:enforcement="1"/>
Check this value before and after manipulation with docx4j.
If it is changing, then something in your code is changing it.
Basically you want something like:
DocumentSettingsPart documentSettingsPart = getPkg().getMainDocumentPart().getDocumentSettingsPart();
documentSettingsPart.protectRestrictEditing(STDocProtect.FORMS, null, null);
See further https://github.com/plutext/docx4j/blob/VERSION_11_4_7/docx4j-core/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/DocumentSettingsPart.java#L311

Fetching translated allowed_values of non-translatable list_string field

I'm attempting to get the allowed_values of a non-translatable list_string field. The allowed_values of this field are translated though and we want to get those allowed_values into a specific language & not the current UI one.
Here is my draft attempt:
// Override language before loading field configuration.
$this->languageManager->setConfigOverrideLanguage($this->languageManager->getLanguage('fr'));
// Load the field configuration in the language forced before.
$field_info = $this->fieldConfigStorage->load('profile.customer.field_title');
$label = $field_info->getLabel();
$allowed_values = $field_info->getSetting('allowed_values');
// Dump values for debugging.
dump($label);
dump($field_info);
dump($allowed_values);
With my current code, I get the proper forced label (here fr) but when I use the ::getSetting('allowed_values') I receive the allowed_values in the current UI language instead of forced one.
Does any one have any idea ?
Many thanks.

Drupal 7 Rules: after generating new content, unable to access a view related to this new data

Using Rules modules, I set up a rule "After saving new content of type X".
So, adding a new content of "X" should:
Based on its nid, call a view, which returns back a JSON, from where I fetch the info to work later. The info I need is on this view, not in the node I am inserting, but to get this view I need the nid and some other info from the node I am creating
The problem is that even if I get the nid of the new node, when I call the view it just returns an empty result. It seems like the data it's still not on Drupal database, so the view results empty. I tried to add a sleep(10) before calling the view, giving some time to Drupal, but no success.
The Content is published, and I added also a 'Save entity' Action to the Rule
Hope with this code here helps to understand:
dsm($node); //I can see al attributes from the node I am inserting
$url="http://localhost/bopa/?q=export_cultivos/$node->nid";
dpm($url);
//it gives me a correct URL, that tested later directly on the browser, works
$data=file_get_contents($url);
$data2 = json_decode($data,true);
dsm($data2);
//EMPTY array
I guess you are passing nid as string not as variable in URL. $node->nid is not getting substituted. So use following code snippet
$url="http://localhost/bopa/?q=export_cultivos/".$node->nid;

Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'PHP-php'

When I trying to load my wordpress site, Its giving an error " Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'PHP-php' ".
1889:
1890:
1891:
Any issue with the following code.
Please help me.
You may ignore this as I have managed to fix the issue. There was a duplicate collection entry of PHP and I have removed the same ;) from domains appPools config file.
Fix: 1. Go to the error occured config file : here, it is C:\inetpub\temp\appPools\yourdomain.com(domain)(2.0)(pool)
take a back up of that (not necessary)
Open in a text editor and find the section containing the website configuration:
Under this section, find and remove the string starting with the collection entry name shown in the error (PHP-php):
Save the config file.
Then give it a try...!!

Take data from a field from exsisting node and make that the default value of a field in different content type

I don't know if I'm on the right track but I'm trying to let users of my web site create there own versions of pages on my web site.
Basically I'd like to make our documentation used as a starting point where they just add details and make a new page for themselves in the process.
I have a 'book' content type that I have changed with CCK and a 'client edits' content type that uses a nodereferencefromURL widget to link itself to the book node.
So simple version of what I'm saying is I have a link on my book pages that creates a node using client edits content type. I would like to put some fields on the client edits content type that take the values of some of the fields from the book page it is linked from.
I'm sure I'm missing something as I would have thought someone would have tried this before but I can't even find a hint on how to go about this.
All I really need is a point in the right direction if my current thinking is wrong.
Current thinking is that I use a php script to get the default value for a field on the new node add screen that drags the value for a field from the book I'm linking from.
I'm thinking this is the case because there is an option for default values for the field in cck manage fields that lets you put in a php value to return a default value for your field.
Am I on the right track or is there already a module or process that does what I'm talking about and I'm just too dumb to find it.
This sounds a little strange, are your client edits going to be a diff from the original node or just coppied data?
I would prehaps do it a more simple way, just have book nodes, and have different fields disaply depending on who edits it (enable the content_permissions module). That way you can use the node clone module to create the users copy.
You will need to make a module to contain your custom php code.
I ended up using rules to save information from the user and the cloned node into hidden fields.
One that saved the original node ID into a field when ever you create content of that type unless the url ends with Clone. This means that when you create the clone the original node ID is kept in the field.
That made it easy to use a views argument that took the node ID to make the clone appear along side the original when a user visits the original page.
The second rule trick was to compute a field that saved the "store name" from the profile of the user only when saving clone content.
This meant that there was a hidden field on the clone that stored the info so I could then use another views argument to restrict the view to only people with the same store name in their profile.
I am no good with PHP but I managed to find a snippet (can't remember where) that returns the store name of the current logged in user as the argument.
global $user;
profile_load_profile($user);
return $user->profile_store_name;

Resources