Tip:
Highlight text to annotate it
X
Ok, go ahead and jump back to the assets folder in your Drupal site. Open up the fifth step
which is called "unmanaged-submit". Copy the entire code and paste it over the code that's
currently in your "assets dot module" file and save it. Ok, in this step, we're going
to bring together a lot of what we've been talking about in the previous videos. Because
at this point, we've validated our file and we actually get to take our file that's in
a temporary directory and move it to a place that's permanent.
In this file, we've added a single function called "assets_unmanaged_file_form_submit".
Now this is a "submit" function for the "assets unmanaged file form" form, and we're triggering
this by using the form API "submit" syntax. Just adding a "underscore submit" to the function
name. And in this function, we're checking to make sure that a file was uploaded. And
we do so by looking at the "values" array inside of "form_state" and we're looking at
"unmanaged_file". Now if we scroll up to our "validation" function, we'll see that we set
this value here to the file that we created using "file_save_upload", so this is in our
temporary directory. Now we just need to move it to the permanent one. So if this file exists,
let's go ahead and do that.
Our first step is going to be making sure that we have a directory created in order
to put our files into. Now by default, Drupal won't create a file directory if we used unmanaged
file functions. So we need to explicitly create a directory. If we didn't, we could just put
the files directly into the default folder but the problem with that, if a lot of modules
are putting their files straight into the default folder, it's going to be a little
hard to figure out what modules are managing which files. Especially if we don't have database
entries. So what we're going to do is create a sub-folder inside of our public files directory
and put our files inside into that. So the location is going to be "assets slash unmanaged"
and then as we move into the managed sections, we'll create a folder called "assets slash
managed". This is our first example of using a URI to point to a file directory. You see
that we're using "public colon slash slash". And this corresponds to the public directory
in our files directory. And we can find out where this is by jumping to our Drupal site
and going to "Configuration, Media, and then File system". And we see here, under the "Public
file system path", that our path is "sites slash default slash files" and we'll talk
about private files in a bit. And you can find out where this located by going here.
Ok, so now that we know where it is, we know that all of our files are going to be uploaded
into this section if we use "public colon slash slash uri scheme".
Ok, let's jump back to our code. Ok. What we're going to do is take this URI to this
folder, and we're going to run it through "file_prepare_directory". This is a function
that will either create a directory recursively, so in this case we have a directory inside
of a directory that we need to create, or it will take that folder if it exists already,
and it will give it the right permissions to write a file to it. In fact, let's go ahead
and take a look at the definition of this function and see what's going on there. I'm
going to go ahead and control click it, which in my editor will take me to the definition.
Here we go. The function takes two parameters. The first is the "directory" and the second
is a constant called "options". By default, it's set to "FILE_MODIFY_PERMISSIONS", which
will, if the directory exists, modify the permissions to allow us to write files to
it, but if it doesn't exist, it won't create it for us. But what we're doing in our function,
is passing it a function called "FILE_CREATE_DIRECTORY", which will create the directory if it exists
and then it will set the proper permissions.
The first step in this function is checking for a valid streamwrapper. In this case, "public
colon slash slash" is our streamwrapper scheme. And then if it exists, we're going to go ahead
and trim the directory name so everything on the right side of that scheme name is going
to be trimmed so that we don't have any extra slashes.
In this next part, we're checking to see if the directory is a directory already. And
if not, we're going to check to see if the "FILE_CREATE_DIRECTORY" constant was passed
as an option here. So what we're doing is checking to see if these two are equal by
using the "bitwise operator and the ampersand symbol" and then surrounding that by parenthesis,
which says "Are these equivalent to one another?". And if so, then we'll cascade to the next
step of this "if statement", which is running "drupal_mkdir", which will create the directory
recursively. If that's successful, then we want to go ahead and change the permissions
on the directory that we just created, so we're running "drupal_chmod". And "chmod"
is a PHP funciton and you'll probably see that if you start to learn or if you work
with shell commands but this is a way for us to set the permissions for the group, the
user and the world. And by just running this as is, we're just using the default permissions
that Drupal has set for chmod.
Let's just take a look at this function real quick. It's a simple one. So I'm going to
control click it and jump to it's definition. Here it is. And now this really simple. It'
checking to see if the URI that we're passing is a directory and if so, we're going to change
the permissions on it to zero seven seven five. Now, you might recognize this from our
initial videos about permissions, default permissions for a folder. Seven seven five
is a good rule of thumb. And this is checking for a variable, which we can set to change
the default permission for a directory if our server is configured differently. And
if this isn't a directory, then we're going to go ahead and use the default permissions
for a file, which is six six four. Ok.
Let's jump back to where we were in the code. Now if we attempted to create a directory,
but it didn't work out, we're going to go ahead and return false. This basically means
that we are passing it something that's not a directory and we haven't passed the constant
to create the directory so this is a situation where Drupal would'nt know what to do who
we're just going to go ahead and return false.
However if we did pass it a directory name, and we've passed the constant "FILE_MODIFY_PERMISSIONS",
this means that what we want to do is prepare the directory to accept files, so we're going
to run Drupal's "chmod" on this directory, but we don't need to create it first. So we
just run this and it will set the proper permissions. And finally, it will return writable, if the
directory is writable already. so before we do any permission changing, we want to check
and make sure that we actually need to do that. So we're using the "is_writable" function
and passing it the directory name to do that.