Drupal hook_file_insert issue - drupal-7

I'm working with the hook_file_insert function and keep running into the same issue over and over again.
Let's take a look at the code:
function mymodule_file_insert($file) {
$src = $file->source // the file field a.k.a field_upload_und_0
file_save_upload($src, array(), 'public://styles/'); // <-- Here's where the issue is.
$file->description = 'Change desc value';
dsm($file) // debug
}
So basically when i upload a file i get this error:
The file MyDocument.doc could not be saved. An unknown error has occurred.
Other than that, I'm also trying to change the description value when the file is uploaded/saved. But that's not happening either. It does however, show as changed in the array when i look in the dsm results.
Am i misunderstanding this function? I suppose it only changes the files array and hangs on to. My thinking originally was that it modifies the actual field values but that doesn't seem to be the case.
How could i grab these values and pass them along in my hook_form_submit function? Is this possible?
Thanks,

The proper solution was to pass the $file variable by reference
function mymodule_file_insert(&$file) {...

Related

Using $scope with $parse

I want to find the value of a parsed text in angular.
For example:
I know i can set the scope value of a variable text like following
var text = 'randomString';
$parse(text).assign($scope, 1234);
But i do not need to set the value. Instead i want to retrieve it. I tried console.log($parse('randomString')), but it prints out a function. I tried console.log($scope.$parse('randomString')), but it throws an error saying it is not a function.
I know i can use $scope.randomString in the above example but i need the string to be dynamic so i do not know what the string will actually be
How can i retrieve the value?
Okay so I came across a solution online. I used:
console.log($scope.$eval('randomString'));

Drupal Views variable $rows outputs as a String

I've developed a custom .tpl.php file for my View and in the past it has worked. Suddenly, while working on my Macbook using MAMP, Drupal decided that the $views->rows needs to be output as a String type and not an array. I've searched online and here for an answer but can't find one. I'm not doing any pre-process or views_render hooks in my template.php file for the theme. Does anyone have any ideas or have seen this before?
Thanks
After going through the Views module code, I couldn't find a _render hook that I would alter to cause the $rows to go back to an Array type. I did go through the modules/views/theme/views-view.tpl.php
So I replaced most of the code in my own template with the views-view.tpl.php code, as well as replacing the database with a previous version so I could start completely over. Turns out the issue was with my template file not outputting the exposed filters and such, as well as Views using
print $rows
instead of using $rows as an array. Seems like whatever version of Views I'm using uses the $rows variable as a String. So I've put in a %SPLIT% string in the Rewrite Results box so that I can do a PHP preg_split, feed that resulting array into my function to generate what I need, then do a preg_replace to get rid of the %SPLIT% strings in $rows. The result looked like what I had.
So, bottom line, looks like Unformatted Fields in Views now outputs $rows as a String variable instead of an array.
I also discovered this when trying to theme a certain row differently if a condition was met. Most things can be done in the Views UI but I couldn't figure out this one like that. I was finally able to do this with yourtheme_preprocess_views_view_unformatted(&$vars) in my template.php file. $rows seemed to still behave like an array there (although it went back to being a string later).
function yourtheme_preprocess_views_view_unformatted(&$vars) {
if ($vars['view']->name == "name_of_view") {
$rows = $vars['rows'];
$newRows = array();
foreach ($rows as $r) {
$test = strpos($r, "string_i_looked_for");
if ($test) {
$newRows[] = "<hr>$r"; // I needed to put in a divider if the condition was met.
}
else {
$newRows[] = $r;
}
}
$vars['rows'] = $newRows; // So that the array of new rows is what will be sent along.
}
}
My actual problem required the divider only in the first instance of the test, so I also used a counter, but I think the example above gives the idea.

Save data from array in variable

SOLVED!
I`m trying save data from array in variable. I have in controller:
$data = array('upload_data' => $this->upload->data());
and I know that in this array are data about uploading file. One with this date is "file_name", and I want to save this value in controller at variable. I try with:
$image_name= $data['file_name'];
But this not working. I use CodeIginter 2.1.3 framework.
Good solution is: $data['upload_data']['file_name'];
Thanks for help!
$data = $this->upload->data();
then $data['file_name'] will work. The way you're doing it you're burying an array inside another array. Pretty sure you could call it with the following but it's still pointless
$data['upload_data']['file_name']
What version of CodeIgniter are you using?
With 2.1.3 it should work - Check out the last few paragraphs of the documentation at: http://codeigniter.com/user_guide/libraries/file_uploading.html
The $data['file_name'] will give you the file name of the uploaded file if it has been successful. To get the full name/path use the other fields in the array.

In CakePHP, how do I go about making the following manipulation to my specific text files, and caching it?

Essentially at the moment I have a text file written in the script of another language. It will be stored as an element, and called to be served up as text when needed.
I want to manipulate this file with the following:
Replace all the existing variables with a php array value storing that variable name. So if the variable looks like #foo, it becomes $variables['foo'].
Create an array out of the entire file, making each line a row in the array.
The result is something like
return array(
'first line of code which has a variable called ' . $variables['foo'] . 'in it',
'second line...'
);
What would the simplest method be to go about this, and is there a way to cache the process, or should I perhaps just save and store the new file some where I can access it?
Thank you!
Use str_replace() (documentation: php.net) to replace all occurences
Use explode() in the following way: How to put string in array, split by new line?
Caching.
I suggest you use View caching, a built in CakePHP helper.
To distinguish between the different caches for the different variable names, make sure you call the controller function with a parameter that discriminates among the variable names. E.g.:
public function generate_script($varname){
// some code
}
If the discriminating variable is not known on calling the controller method (but is e.g. determined inside the controller method), you should look into the Cache API

Accessing variable from other class returns null

I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?

Resources