Can anyone tell me if it is possible to mix views arguments with static strings?
For example in the path section of a view feed display I need:
/mypath/%.xml
with the ".xml" part being the static string.
Thanks in advance.
I figured it out finally.
Under validation, choose PHP code. Then I entered:
// strip ".xml" from incoming
$new_arg = preg_replace('/\.xml$/', '', $argument ); argument
$handler->argument = $new_arg;
return TRUE; //must return something
That works. Now Drupal sends "foo" to the SQL query, even if the incoming argument via the url is "foo.xml"
I just tested this, and you can't do the exact path you posted above. Views appears to only recognize '%' as an argument placeholder if it sits between slashes, or by itself at the end. So, what will work is something like this:
/mypath/%/rss.xml
or
/mypath/static/%
In path, anyway, you should set path/%
But you can check argument %.xml in validating code:
In views argument additing/editing window:
Validator options - Validator - PHP Code:
Enter PHP code that returns TRUE or FALSE. No return is the same as FALSE, so be SURE to return something if you do not want to declare the argument invalid. Do not use . The argument to validate will be "$argument" and the view will be "$view". You may change the argument by setting "$handler->argument".
Use strpos to check if there xml string.
Also you can modify argument as it wrote in comment: $handler->argument
Related
I'm trying to create an endpoint that contains an actual path that I extract and use as a parameter. For instance, in the following path:
/myapi/function/this/is/the/path
I want to match "/myapi/function/" to my function, and pass the parameter "this/is/the/path" as the parameter to that function.
If I try this it obviously doesn't work because it only matches the first element of the path:
app.get("/myapi/function/:mypath")
If I try this it works, but it doesn't show up in req.params, I instead have to parse req.path which is messy because the logic has to know about the whole path, not just the parameter:
app.get("/myapi/function/*")
In addition, the use of wildcard routing seems to be discouraged as bad practice. I'm not sure I understand what alternative the linked article is trying to suggest, and I'm not using the query as part of a database call nor am I uploading any information.
What's the proper way to do this?
You can use wildcard
app.get("/myapi/function/*")
And then get your path
req.params[0]
// Example
//
// For the route "/myapi/function/this/is/my/path"
// You will get output "this/is/my/path"
I am using url module, which basically splits a web address into readable part.
var data = url.parse(request.url).pathname
the output of request.url is C:\AppFolder\dropbox\videos\myVideo8#.MP4. After its get parsed, I dont understand why its not returing the value with "#.MP4"
I dont understand why its not returing the value with "#.MP4"
Because #.MP4 is the fragment and not the path component of the URL. (You can read up on URL syntax f.e. on WikiPedia, if you are not sure: https://en.wikipedia.org/wiki/URL#Syntax)
You want to look at hash, not pathname https://nodejs.org/docs/latest/api/url.html#url_url_hash
Follow up question: What is the code "res.json(false);" doing? Doesn't that print out false on the page instead of showing the data I want?
I'm looking at the following sample code. I understand that .get( is the method and /:characters? is the server path. In this search, what is the point of the colon and the question mark in the path? Shouldn't the question mark come before characters because it is a query?
app.get('/:characters?', function (req, res) {
var chosen = req.params.characters;
if (chosen) {
console.log(chosen);
for (var i = 0; i < characters.length; i++) {
if (chosen === characters[i].routeName) {
res.json(characters[i]);
return;
}
}
res.json(false);
} else {
res.json(characters);
}
});
In this case the question mark signifies an optional parameter "characters". This allows for an endpoint that MAY have a value or not. They are then testing against that parameter to see if it was included. If so they will iterate through the "characters" object and return any matching entry to the endpoint the user specified.
Simplest answer:
:XXX means that its a URL parameter. (i.e. req.params.XXX will pick up what the XXX is)
? means that the parameter is optional. (i.e. the client-side user doesn't need to include this parameter in the url).
So:
/:characters? would allow for: both / AND /yoda to hit this route.
:characters isn't actually part of the query string. It will be part of the url.
The url will be something similar to the following (assuming you are running this server locally and on port 8080):
http://localhost:8080/abcdefg
And in that case, req.params.characters will be 'abcdefg'
Putting an explicit question mark in the route definition is a mistake, in my opinion. I'm not entirely sure what purpose that question mark would serve.
For the follow up question, what it appears to be doing is looking for a match in the characters variable (which I assume is defined externally) by characters[i].routeName, and returning the found value. If no value is found, that's when it sends back false (or tries to - to be honest, I'm not sure what express will do if you try using res.json(false), since I'm not sure false is valid JSON).
This is a normal case that a endpoint or a resource can contain a variable that means
lets take facebook for example so if you see the timeline of MarkZukerberg then the endpoint or url is
https://www.facebook.com/zuck
if you see your own profile then that zuck will be replaced by your name. so the name parameter here is a variable.
When ever a variable is a part of the url then we precede that with a colon sign in express syntax
and if we want to send some other values as the query parameter then we use ? mark to tell the server that the following string will be a query parameter
?name=value&age=12&gender=male
And
res.json(false)
will just return false as the response nothing else
I am getting
Error: [$location:ihshprfx] Invalid url "http://localhost:3000/#", missing hash prefix "#!". http://errors.angularjs.org/1.2.16/$location/ihshprfx?p0=http%3A%2F%2Flocalhost%3A3000%2F%23&p1=%23
after delete function:
$scope.delete = function(venue) {
venue.$remove();
};
but venue is successfully removed. What could be wrong?
UPDATE: I saw that in a dropdown I wrote href = '#' . But when I delete it, nothing happens. How I can call directive from a tag?
Delete
Your ng-click calls a function called remove while your scope method is titled delete. Because it can't find the function you've set, I believe Angular doesn't to prevent the default event, which sends the user to '#', which is not valid based on your hash prefix settings.
If you match the function call to the method name, I think you should be fine.
In my custom form (in a custom module) drupal_add_js() only adds the JS when there is no error message.
My code goes like this:
function ntcf_redo_order_form( &$form_state = array() ) {
global $base_path, $user;
$my_dir = drupal_get_path('module', 'ntcf_redo');
drupal_add_js("$my_dir/order.js", 'module', 'header', FALSE, TRUE, FALSE);
$form = array();
...
return $form;
}
If the validation function used _form_set_error()_ to display an error message and highlight the offending field, the message is displayed and the field highlighted, but the _drupal_add_js()_ call does nothing. Without a pending error message to display, all is well.
EDIT: this problem does not occur with drupal_set_message(), only with form_set_error().
I tried adding the 3 later parameters to the *drupal_add_js()* call to tell it to not optimize it (don't combine it with other JS files). There is no mention of the file order.js in the HTML, and it makes no difference whether I use the last 4 parameters ('header', FALSE, TRUE, FALSE) or not.
In Admin/Performance, I turned off Optimize Javascript Files, and pretty much all caching, which also made no difference.
Extra Details:
I'm not sure if this makes a difference, but it wouldn't surprise me, so here goes:
What I'm doing here is a multi-part "wizard" form that allows the user to proceed forward and go back. Also, many of the pages use AJAX, so I need to do all the "required" field validation in the _submit function instead of letting Drupal do it automatically (since that makes a mess of AJAX). So, if there's a "required" field that's missing, the _submit() function sets an error message, and the form generation function generates the same form again (with the additional decoration resulting from the error message).
Also: this is off-topic, but it might help someone using Google: when doing a multi-page form that allows going backward, you MUST assign a weight to every item on the form, or else the fields tend to "wander" when you go backwards.
Any ideas?
I had the same problem, this is a workaround I found (for Drupal 7, may work in 6) :
1. in your form setup (or hook_form_alter), do this :
$form['#post_render'][]='yourfunction';
2. define :
function yourfunction($content,$element){
$my_dir = drupal_get_path('module', 'ntcf_redo');
drupal_add_js("$my_dir/order.js", 'module', 'header', FALSE, TRUE, FALSE);
return $content;
}
I think this works (while your approach does not), because hook_form_alter (and/or hook_form)
do NOT get called again for a prepared/cached form, so the initial form load WILL load the javascript, but subsequent posts will NOT.
HTH
Mikes answer ($form['#post_render'][]='yourfunction';), will work, though its not the optimal way and will cause issues with drupal_add_js.
The best way to do this is by adding your javascript via the form api '#attached'.
Instead of using drupal_add_js or a new callback on the '#post_render':
$form['#attached']['js'] = array(
drupal_get_path('module', 'module_name') .'/file/path/filename.js',
);
You may pass in a 'css' array as well. Being an array, you can pass in as may files as you want.
*This is for Drupal 7. Other versions may be different.