Get Image path in drupal - drupal-7

may be this question has been asked earlier but i have not found an appropriate answer for this. I have image uri path and i want to create a web services in drupal. From android when i call this webservices i will send the uri then i want to get the image url that will return me my custom image style url. But when i am passing the url with file extension giving me Could not find the Controller
This is my function that will return the url of the image:
'image_url' => array('operations' => array('retrieve' => array(
'help' => t('The resource to retrive the image of the given url/uri.'),
'callback' => '_fieldinsync_image_url_retrieve',
'args' => array(
array(
'name' => 'url',
'optional' => FALSE,
'source' => array('path' => 'url'),
'type' => 'string',
'description' => 'The url/uri of the image.',
)),
'access callback' => 'services_access_menu',
),),),
Function:
function _fieldinsync_image_url_retrieve($imgUrl){
$value;
$value[]=array(
'vname'=> 'session_id',
'sname' => 'session_name');
$value['uri'] = image_style_url('mobile_product_image', $imgUrl);
return $value;
}
Uri is :public://Tab - A2107.jpg

got my Answer
needs to add these lines in settings.php
$conf['image_allow_insecure_derivatives'] = TRUE;

Related

drupal form field not loading correct data

I am building my first Drupal 7 module and am having trouble with the screen to edit a fieldable entity. I am using field_attach_form and it is working great for all accept one field which is displaying the field default rather than the current content of that field for that entity.
I have a text field, a number field, a number of Boolean fields and the one list_text field which is failing.
Any ideas what I a doing incorrectly? Code below is what I think is needed but please do let me know if you need more.
Code to create the field in hook_enable:
if (!field_info_field('field_available')) {
$field = array (
'field_name' => 'field_available',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('No', 'Provisionally', 'Yes'),
),
);
field_create_field($field);
Code to create the instance, also in hook_enable:
if (!field_info_instance('appointments_status', 'field_available', 'appointments_status')) {
$instance = array(
'field_name' => 'field_available',
'entity_type' => 'appointments_status',
'bundle' => 'appointments_status',
'label' => t('Available?'),
'required' => TRUE,
'default_value' => array(array('value' => 'No')),
'description' => t('Set to No if appointments with this status make this slot unavailable, Provisionally means that it will only reserve a space temporarily'),
);
field_create_instance($instance);
This entity has only the one bundle with the same name as the entity.
The code to create the URL in hook_menu:
$items['admin/appointments/appointments_statii/%/edit'] = array(
'title' => 'Edit appointment status',
'description' => 'Edit the parameters of the selected status code',
'page callback' => 'drupal_get_form',
'page arguments' => array('appointments_status_edit_form',3),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
The form function is:
function appointments_status_edit_form($form, &$form_state) {
// Get the status id from the form_state args
$status_id = $form_state['build_info']['args'][0];
// Load the chosen status entity
$status = entity_load_single('appointments_status', $status_id);
// Set up the fields for the form
field_attach_form('appointments_status', $status, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save changes',
'#weight' => 99,
);
return $form;
}
I have used the Devel module's dpm to check that the data is loaded correctly by entity_load_single and it is.
Thanks
Rory
I have answered my own question!
I was also programmatically loading some entities and was not loading this field with the numbers that a list_text field stores, instead I was loading the visual text.
I used a metadata wrapper and the code looked like this:
$w_appointments_status->$appointments_availability= 'Yes';
I changed it to:
$w_appointments_status->$appointments_availability = 2;
In this example 'Yes' was the third allowed value - hence 2.
So the code in my question was in fact correct although I have since added 'widget' and 'formatter' parameters to the instance.
I am sorry if this got some of you scratching your heads thinking ' but that code is correct'!!
Regards
Rory

Is there any way to define a new route in druapl 7 which start with wildcard

I implement a hook_menu and I want define a route like this
$item['%'] = array();
and when user enter url like this domain/whatever the word whatever must pass as an argument to page callback function. can any body help me please.
There is no way to have a fully dynamic route in Drupal, most modules (including core) loop on content_type to create these kind of routes
foreach (entity_get_info() as $entity_type => $entity_info) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$path = $bundle_info['admin']['path'];
$items["$path/fields"] = array(
'title' => 'Manage fields',
'page callback' => 'drupal_get_form',
'page arguments' => array('field_ui_field_overview_form', $entity_type, $bundle_arg),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'file' => 'field_ui.admin.inc',
);
}
}
Example at http://cgit.drupalcode.org/drupal/tree/modules/field_ui/field_ui.module?h=7.x#n79

Can hit Drupal 7 REST resource endpoint but not with the CRUD operation added

I have a REST endpoint defined using the D7 services module. I have enabled the service and the CRUD operation. I have enabled the permission. When I hit the url for the endpoint, /myservice, I get a message
Services Endpoint "myservice" has been setup successfully.
When I hit /myservice/create (it's the Create CRUD service I have enabled), I just get a blank page, though the callback, below, has a print statement.
.module
function myservice_permission() {
return array('create constructs' => array(
'title' => t('create constructs'),
'description' => t('Receive messages'),
)
);
}
function _myservice_access($ops, $args) {
return TRUE;
}
function myservice_services_resources() {
return array(
'myservice_messages' => array(
'create' => array(
'help' => 'Creates messages',
'callback' => '_myservice_create',
'access callback' => '_myservice_access',
'access arguments' => array('create constructs'),
'access arguments append' => FALSE,
'args' => array(
array(
'name' => 'data',
'type' => 'struct',
'description' => '',
'source' => 'data',
'optional' => TRUE,
),
),
),
);
}
function _mymodule_create($data) {
print '***here';
}
function myservice_services_endpoint() {
$endpoints = array();
$endpoint = new stdClass();
$endpoint->disabled = FALSE;
$endpoint->api_version = 3;
$endpoint->name = 'myservice';
$endpoint->server = 'rest_server';
$endpoint->path = 'myservice_message';
$endpoint->authentication = array();
$endpoint->server_settings = array();
$endpoint->resources = array(
'myservice' => array(
'operations' => array(
'create' => array(
'enabled' => '1',
),
),
),
);
$endpoint->debug = 1;
$endpoints[] = $endpoint;
return $endpoints;
}
In the services admin panel I have the resource and the crud operation enabled.
One related thing I should ask: there are 4 named items, endpoint, resource, service and endpoint path. Do all have to have different names?
It was the way I attempted to test. I completely spaced that I was testing a POST transaction and not a GET.
FYI, the easiest way to test non-GET transactions are with a helper. For chrome, I downloaded Advanced Rest Client. Actually sending post data to a service configured to process POST is the best way to test it :)

Trouble Generating PDF with CakePHP and CakePdf

I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
'orientation' => 'landscape',
'download' => false
));
I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.
In the viewReport function of CourseGradesController, I have
function viewReport($id = null)
{
$this->CourseGrade->id = $id;
if (!$this->CourseGrade->exists())
{
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->CourseGrade->read(null, $id));
...
}
If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."
If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.
I would strongly recommend the use of this plugin for this kind of work.
https://github.com/ceeram/CakePdf

Cakephp Error using Miles J Uploader plugin

I am currently trying to use Miles J plugin located here http://milesj.me/code/cakephp/uploader Although I have made great progress learning CakePhp, I am currently having a problem using the plugin and I would appreciate any help provided.
I have followed all the necessary steps to use the plugin. It has been downloaded put on Plugin folder, I bootstrapped with CakePlugin::loadAll().
So far so good.
Next I have proceed to set up the table as indicated by the plugin developer.
Ok, now back to my own code. I have the following set up:
images_controller.php , image.php and their views.
My goal now is to use the plugin inside those files as such:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Now, my model is set as follows
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array(
'value' => array('gif', 'jpg', 'jpeg'),
'error' => 'Only gif, jpg and jpeg images are allowed!'
),
'minWidth' => 500,
'minHeight' => 500,
'required' => true
),
'import' => array(
'required' => false
)
),
'Uploader.Attachment' => array(
'file' => array(
'name' => 'uploaderFilename',
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
// Save additional images in the databases after transforming
array(
'method' => 'resize',
'width' => 100,
'height' => 100,
'dbColumn' => 'path_alt'
)
),
'metaColumns' => array(
'size' => 'filesize', // The size value will be saved to the filesize column
'type' => 'type' // And the same for the mimetype
)
),
'import' => array(
'uploadDir' => '/files/uploads/',
'name' => 'uploaderFilename',
'dbColumn' => 'path',
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
array(
'method' => 'scale',
'percent' => .5,
'dbColumn' => 'path' // Overwrite the original image
)
)
)
)
);
}
}
On my model for testing purposes I have not changed anything but just copied and pasted the very same array as shown in the the Test/Model folder inside the plugin, which is meant to show the functionality of the plugin.
The following confusion, errors or lack of understanding is taking place:
My file is not being uploaded to the webroot/files/uploads folder
My data is being inserted in the database, but not in a complete manner by leaving empty as shown:
id | caption | path | path_alt | created |
4 | | | |2012:02:..|
Above, I expect the path to be saved, but it doesn't.
I have to admit my confusion comes mainly from my inexperience using plugins, so I am aware I might be doing something wrong regarding my models or my configuration.
Any prompt help would be appreciated greatly as I have tried to work this out on my own without any success.
A few things:
1 - In your controller you do not need to import the Uploader class. You also don't need to use the Uploader.Upload model (it's merely an example test case). All you need to do in the controller is call $this->Image->save() which will upload the file and save the path as a row into the database (if you defined the Attachment in Image).
2 - In your view, create the file input. Pay attention to the input name.
echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));
3 - In your Image model, setup the AttachmentBehavior and its options for that specific input field:
'Uploader.Attachment' => array(
'FILE_INPUT_NAME' => array(
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path'
),
'ANOTHER_INPUT' => array()
);
Be sure that the column "path" exists in your images table. And thats it.
For more information on what each option does, check out the following: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model

Resources