Silverstripe 4 get draft files in query - silverstripe

I'm very new to Silverstripe. I want to make draft files visible to logged in users.
When I execute a query like
File::get()->Where($query)
I only get published files.
How do I get all files for the logged in user with the permissions to view the file?
Thanks for your answer

Never tried this with files, but you could try querying them via Versioned::get_by_stage()
$draftFiles = Versioned::get_by_stage('File', Versioned::DRAFT)->where($query);
$liveFiles = Versioned::get_by_stage('File', Versioned::LIVE)->where($query);
See here for more detail.

Thanks Aaryn,
this works fine with Files,
but I dont get it work with a many_many relation saved in a
DataObject.
when iterating through the dataobject the many_many field is always NULL.
in this field i saved uploaded, unpublished files.
foreach ($sitzungen as $item) {
if ($item->MDateien() != NULL) {
...
}
...
}
$sitzungen is the DataObject.
MDateien is the many_many field.
Thanks for your answer

Related

CategoryManyToManyField tuns to emty QuerySet in non edit mode

I write a new plugin for aldryn_newsblog application. I want to list articles by categories. The plugin takes the list of categories using CategoryManyToManyField.
The plugin displays the articles having requested categories only in edit mode. As soon as I publish page with newly added plugin it displays none of the articles. I checked that self.categories.all() becomes empty.
Why it is happening?
This is for;
aldryn-newsblog2.1.1
Django==1.11.17
django-cms==3.5.3
It acts the same on local django development server and on remote apache
class NewsBlogCategoryPlugin(PluginEditModeMixin, AdjustableCacheModelMixin, NewsBlogCMSPlugin):
....
categories = CategoryManyToManyField('aldryn_categories.Category', verbose_name=_('categories'), blank=True)
....
def get_articles(self, request):
print (self.categories.all())
queryset = Article.objects
main_qs = queryset.all().filter(categories =
self.categories.all())
return main_qs
I believe that the issue is this:
when your page is published, the plugin instance is copied
in the published version of the page, there is now a new plugin instance
this new plugin instance does not have the relations to the Categories that the original did
You need to ensure that when a plugin is copied, so are its relations.
See handling relations in the django CMS documentation.
Thank you #Daniele. I just have added following the handling-relations
def copy_relations(self, oldinstance):
self.categories = oldinstance.categories.all()
And now it works

Drupal 8: Mismatched entity and/or field definitions

While trying to understand why my view is not displaying, I noticed the following error in the log:
I do not think it is possible to delete the URL alias from Taxonomy terms. At least I cannot find how to do this.
I have, however gone through ALL of my taxonomy terms and removed the value for this field.
I have also done the following with Pathauto:
Also, I have checked the report located at admin/reports/fields and can confirm that there are no entities that use a field called URL alias.
I have gone through each content item and ensured that they have the following setting (anyone know how to do this in bulk?). But still the error remains.
Anyone know then how I can fix this strange error?
Im not entirely sure what this command does, but it fixed the error:
drush updb --entity-updates
Since https://www.drupal.org/node/2554097, the magic in Drupal core that took care of updating entity definitions is gone. drush updb --entiy-updates is an alternative to this but it is not a silver bullet. Instead, it is safer to write database updates.
Taking the screenshot at the top as an example, here is a database update that would delete those two field definitions:
/**
* Fix taxonomy and node field definitions.
*
*/
function mymodule_update_8101() {
$manager = \Drupal::entityDefinitionUpdateManager();
if ($field = $manager->getFieldStorageDefinition('alias', 'node')) {
$manager->uninstallFieldStorageDefinition($field);
}
if ($field = $manager->getFieldStorageDefinition('alias', 'term')) {
$manager->uninstallFieldStorageDefinition($field);
}
}
Have a look at the rest of the available methods at https://www.drupal.org/node/2554097 in order to write database updates for each scenario.
use the entity_update module or the devel_entity_updates module

Drupal user permissions & odd content types

I have a permissions problem in Drupal. I want users to be able to create a certain node type, but there are two different paths I need to give them permissions for to let them do this. The type is content created by a module called isbn2node, and there are two ways to make content through it, each with different paths:
?=node/add/isbn2node-book
?=node/add/isbn2node_book/isbn2node
One has an underscore and the other one has a hyphen. The first path leads to a form that lets users enter information on a book manually; the second path lets them enter an ISBN, searches for it, and populates the form for them based on the results.
I've changed permissions in the People menu so they can add isbn2node-book content manually using the first path, but there isn't an option to let them use the second method. Aliasing the url so it didn't have node/add in the path didn't work either.
Creating a duplicate content type seems like an ugly solution to this; is there a more elegant way to let users access that second path?
A little code in a custom module using hook_node_access should do it.
$node is either a node object or the machine name of the content type on which to perform the access check (if the node is being created then the $node object is not available so it will be a string instead).
So this should do it:
function MY_MODULE_node_access($node, $op, $account) {
if ($op == 'create') {
$type = $node;
if($type == 'book' && $account->uid) return NODE_ACCESS_ALLOW;
}
}
I figured this out, and the issues I was having were specific to this content type. The ISBN2Node module requires users to have the Administer Nodes permission to use its lookup and bulk import features.
There is some extra code for the module's hook_permission and hook_menu sections submitted as a fix in the module's issues thread.

Give edit permissions to roles based on taxonomy and content type

I'm looking for a way to manage edit permissions on Drupal 6 nodes based on the content-type of the node and on his taxonomy.
For example, I'd like to be able to allow a role to edit stories of taxonomy1 and pages of taxonomy2 but not stories of taxonomy2 and pages of taxonomy1.
I know how to restrict access by content type or by taxonomy (with the taxonomy access permissions module), but I can't manage to grant permissions on both of these criteria. I would greatly appreciate a module suggestion or some ideas on how to make it programmatically.
Thanks in advance for any hints !
function hook_menu_alter(&$items) {
$items['node/%node/edit']['access callback'] = my_permission_function;
}
function my_permission_function($node){
if( (node_type is 1 and tax_of_node is tax2)|| (node_type is 2 and tax_of_node is tax1)
return TRUE;
else return FALSE;
}
Not the exact code. Follow the logic. Try something like this and it should work.

Re-processing attached images in drupal 7

I'm trying to import nodes from my forum to drupal 7. Not in bulk, but one by one so that news posts can be created and referenced back to the forum. The kicker is that I'm wanting to bring image attachments across as well...
So far, using the code example here http://drupal.org/node/889058#comment-3709802 things mostly work: Nodes are created, but the images don't go through any validation or processing.
I'd like the attached images to be validated against the rules defined in the content type. in particular the style associated with my image field which resizes them to 600x600.
So, instead of simply creating the nodes programatically with my own form, i decided to modify a "new" node using hook_node_prepare and using the existing form to create new content (based on passed in url args). This works really well and a create form is presented pre-filled with all my data. including the image! very cute.
I expected that i could then hit preview or save and all the validation and resizing would happen to my image, but instead i get the error:
"The file used in the Image field may not be referenced."
The reason for this is that my file doesn't have an entry in the file_usage table.. *le sigh*
so, how do i get to all the nice validation and processing which happens when i manually choose a file to upload? like resizing, an entry in the file_usage table.
The ajax upload function does it, but i can't find the code which is called to do this anywhere in the api.
What file upload / validation functions does Drupal call which i'm not doing?
Anybody have any experience with the file/image api for Drupal 7 who can help me out?
For getting the usage entry (in essence, checking out a file to a specific module so that it doesn't get deleted while its in use) look up the Drupal function 'file_usage_add()'
For validating incoming images, I got this example from user.module (if you're comfortable with PHP, you can always look at the core to see how something is done the 'Drupal way'):
function user_validate_picture(&$form, &$form_state) {
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
// Save the file as a temporary file.
$file = file_save_upload('picture_upload', $validators);
if ($file === FALSE) {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
elseif ($file !== NULL) {
$form_state['values']['picture_upload'] = $file;
}
}
That function is added to the $form['#validate'] array like so:
$form['#validate'][] = 'user_validate_picture'

Resources