Active
Project:
Location
Version:
6.x-3.1
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
23 Feb 2009 at 20:57 UTC
Updated:
2 Feb 2016 at 01:29 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
Dave Cohen commentedThe attached patch emulates the logic found earlier in _location_expand_location(), where it sets the title only if it was not set in an earlier form_alter():
The patch checks whether delete_location has been set before setting it. I think it would be cool to make a similar check before anything is added to the element.
With the attached patch, the following logic in my hook_form_alter allows me to hide the delete checkbox from normal users:
I post this in case it is helpful to others.
Comment #2
Dave Cohen commentedAlso, the delete checkbox appears on forms where parts of the location are required. In my opinion, if I've set things up to require a location, delete should not be an option.
Comment #3
Dave Cohen commentedOk, one more suggestion. Since lots of people seem to want to alter the location element, add some code like this to the end of _location_expand_location()
...right before the $element is returned.
In Drupal 6, this would be simply a call to
drupal_alter('location_element', $element)I think this would kill many birds with one stone, and is consistant with other parts of Drupal. I'm using this code now to change the text associated with the locpick elements. And this would suffice in place of the patch I submitted earlier.
Comment #4
yesct commentedTagging with a theming tag for now.
Comment #5
Dave Cohen commentedSince Cameron asked me for more details about my earlier comment...
The idea is to add the above code to the function _location_expand_location(). Then, in your own module, implement MY_MODULE_location_element_alter(&$element). In that function, make whatever changes you might have made in hook_form_alter(), if the location element did not have its own special processing.
You need some PHP chops to have this work for you, not just CSS. However in your hook_location_element_alter() you could add a #prefix and #suffix to wrap the element in a div, which could then be used in CSS.
Here's my implementation of hook_location_element_alter(), which adds some instructional text...
Comment #6
rc2020 commentedAwesome, I got the code implemented in a module I made called formtheme.module which also uses hook_form_alter() to modify node forms. However, I'm trying to implement this on a case by case basis, so on one node type form the location fields are themed like x, and on another node type form the location fields are themed like y. I figure the most logical approach to this would be a 'switch' statement, but I'm not sure what to switch on. Going by hook_form_alter() I added the $form_id as a parameter at location_element_alter(), switching by $form_id, but it doesen't seem to be working. Here is my code
I'd like to make it contextual by $form_id like I can with hook_form_alter(), do you know a good way to go about doing this?
I appreciate the help very much!
Comment #7
zmove commented/subscribe for the #3 addition.
Tested, works, I think it can be commited to allow to alter form element without having a hacked location module;
Comment #8
yasir farooqui commentedExcellent Dave Cohen, this really worked.
Comment #9
tamasco commentedHello,
@Dave Cohen, I want to remove the 'delete location' checkbox from all forms. Module version is 'Location 6.x-3.1-rc1'. I have applied the patch you provided at 1 but I don't know what to do next. I'm a beginner in both PHP and Drupal. I have no idea about how to use the code for hook_form_alter. I don't even know where hook_form_alter is located.
Any help will be very much appreciated.
Thanks.
Comment #10
Dave Cohen commentedTomasco, I don't have time to get you all the way there. But I'll share a snippet from my hook_form_alter. Look on drupal.org for how to write a module and implement hooks. And be warned this code is for Drupal 5.x. So for a 6.x site you will have to change the parameters passed into the function and more...
Comment #11
tamasco commentedThanks a lot, Dave.
Comment #12
nirbhasa commentedHere is a patch containing Dave's one line calling of drupal_alter for D6, worked like a charm for me. It would be great to get this in for D5 and 6
Comment #13
jsenich commentedThe above patch for D6 doesn't work for me. I still can't access the location form fields in my hook_form_alter implementation after installing the patch.
Comment #14
nirbhasa commentedThe patch creates a new hook called hook_location_element_alter
Within that hook, you can access the fields as $element['country'] etc. (edit - see comment #6)
Comment #15
jsenich commentedAhhhh, that works! I skimmed through the comments too quickly and didn't piece it all together. Thanks!
Comment #16
hefox commentedthough the alter is nice, I believe there's already a core drupal 6 way in: after_build.
The issue with altering elements in form alter is that the element hasn't been processed.
However form calls after build functions after elements are processed(expanded) and before they're rendered.
(Another example of an after_build on my 'blog' http://foxis.intheclosets.com/text/adding-attributes-changing-cck-field )
Haven't tested if can get the delete box gone, but should be able to.
Comment #17
Dave Cohen commentedI agree that one of those uber-advanced triple-secret form trick likes
#after_buildis the way to go, if it can be made to work. I personally don't have time to figure it out.The module could provide the alter hook as a convenience. But its reasonable to not do so if Drupal core already provides a way.
Comment #18
hefox commentedDoing a quick test it seems to work :>.
assume above has been done so after_build function is called
Assume field_location is the cck location field.
in the after_build function
or for multi value:
It's import to return $form or else the $form array will disappear (the after_build functions are called as $form = after_build_function = $form).
(Why I did access instead of unset or set to array: I prefer #access when removing an element on a forum so it's still there in the array in case a later piece of code expects it to be there it'll be there. Access will disallow access to an element; I believe it changes the element to a 'value' instead of whatever it was. For example #access = 0 on the author field, the author field will turn into 'author'=>array('#type'=>'value','#value'=>'whatever the value (default value) was?'). I believe it works the same in after_build as in form_alter.
Haven't tested to see how it'd be done with location_node or location_user, just location cck, but I bet something similar.
Comment #19
scottrigbyHi Guys,
I can't seem to get this to work for location cck - nothing in dpm($form['field_profile_location'][0]['delete_location']) at all - curious
Comment #20
scottrigbyActaully - there is no linked patch in #12 (simply goes to http://drupal.org/files/issues) so perhaps I'm missing an important piece of the puzzle. Can someone clarify? I will be happy to test, re-roll patch, pitch in, etc
Comment #21
hefox commentedScottrigby, what steps did you follow exectly?
Are you trying to do the alterations in just form_alter? If you are, then the field hasn't been built so there would be no delete_location yet.
Comment #22
scottrigby@hefox, yes - i think i was unsure about the state of this patch, or what patch was RTBC, or if wasn't a patch at all finally but a few possible solutions. Now i gather the patch in #12 was necessary :p -- was that just an expansion of #3?
Comment #23
hefox commentedI think so, however I never tried that patch since as detailed above, the location element can also be reached in an after_build function, so I'm not sure if the patch will make it into module (even if figures itself out and reappears D:!)
Comment #24
nirbhasa commentedHi - I uploaded that patch in #12, but not sure what has happened to it, I can confirm that it is just #3 in patch form.
Comment #25
chucklima commented#16 worked for me.
No hacked location.module. Don't forget 'return $form' !
Also, use loop for multi-location nodes.
Thanks Dave Cohen and hefox.
Comment #26
malks commentedJust wanted to say #16 worked for me. Thanks hefox for pointing out #after_build, it's great! If I hadn't got this working I would have had to throw out Location for no better reason than the labels were wrong. Pesky customers.
Thanks again.
Comment #27
yesct commentedsoooo no patch is needed? the solution in #16 will work?
Comment #28
richardtmorgan commentedSolution in #16 absolutely works.
I have locations set for a number of content types, and so I used:
which will match any $form_id with 'node_form' in it and then add the function change_location_fieldset() [which you define - the name is irrelevant] to the list of functions that are called after the form is built (when all the location items are available to be altered).
--
Another tip was that I called:
die (print_r($form));as the first line inside my #after_build function (here 'change_location_fieldset()') to dump the $form array in my browser window. Then selecting 'view source' lets you see a nicely laid out version of the array. For me this is the quickest way of seeing the variables I needed to change. Once you've got the list of things you want to change, you remove the line.
--
Changes to labels (and descriptions) are straightforward, i.e.
$form['locations'][0]['name']['#title'] = 'Name';--
I had an issue with the 'additional' field showing before the street line. It seems that the weight of the additional field is set to the same as the weight of the street field. On the node I looked at (with
die(print_r($form))) they both had a weight of -99, so I set the weight of additional to -98 and the problem went away. However, on a different node form, where the location field was displayed in a different place, this had the effect of floating the additional field to the top of the fieldset.The reliable solution to putting the additional field underneath the street field for me was simply to make it one heavier than the street field:
$form['locations'][0]['additional']['#weight'] = $form['locations'][0]['street']['#weight'] + 1;--
My complete code (for the changes I wanted - it needs to be put in a module) is:
have fun - this is really very easy (as easy as hook_form_alter() - but with the one additional step of having to put your changes in a function that is called once the form is built)
Comment #29
yesct commentedadding tag so someone might find this snippet of useful code.
Comment #30
dboulet commentedIt seems to me like the module itself should contain logic that decides when to hide the 'Delete' check box—there are some cases where having it just doesn't make sense. Consider for example the case where I've set both the maximum and minimum addresses for a node to 1, and the address contains required fields.
Comment #31
yesct commenteddboulet, are there other programmable cases?
What happens in your case, does it work to delete the location even though it is required?
I wonder if this might be too much of an edge case. Using form alter is pretty easy and common.
Is the need for a UI for folks who are uncomfortable trying to make their own module to do a form alter?
Maybe a generic checkbox config for "provide a Delete check box' somewhere in the location config? Where do people think that type of thing might go?
guess I'm tempted to mark this wont fix, because at first thought, it seems like an edge case. (from a module perspective) Or at least postponed as there might be more generally useful things to work on location (like proximity search and views).
Comment #32
dboulet commentedI couldn't think of any other cases, but thought that others could post some.
In my case, yes, I can still delete a location even though it is required. If I try to edit the node again, the fields are blank and I'm forced to re-enter the required fields in order to save the node. I don't think that this scenario, one where a node type contains exactly one location which itself contains a required field, would be described as an edge-case, I would assume that it would be a pretty common configuration.
Either way, I just wanted to get a discussion going, and see what others thought.
Comment #33
Dave Cohen commentedEarlier example in this thread work by setting $form['#after_build'], which affects the entire form. Here's a way to set '#after_build' for only the location elements, which I personally prefer.
In my case there is only one location fieldset.
Finally, am I wrong to think this issue is resolved? Setting it to fixed.
Comment #35
rfiertek commentedHi All,
I tried to implement these methods but I get this error.
Fatal error: Unsupported operand types in /home/topseque/public_html/rb4/includes/common.inc on line 2937I define the $form['#after_build'] inside my hook form alter. After this function is closed, I call the named function, as described above.
It seems to hang on the '#', because when I remove it it no longer brings up the error.
Also, once it gets to my custom named function, the following line causes an error as well. The 'node' comes from when I printed out the output to see how the variables are called.
$form['node']['field_rest_location'][0]['phone'] = '12345';creating this error:
Fatal error: Cannot use string offset as an array in /home/topseque/public_html/rb4/includes/form.inc on line 990Any ideas on what might be going on or what is wrong?
Thanks,
Rob
Comment #36
mandclu commentedFor my part showing a delete checkbox on a location form when it is required should be classified as a bug. Definitely not an edge case, simple common sense.
It's great that workarounds exist (including some that are hack free) but the module shouldn't be providing an invalid option in the first place.
Comment #37
Berliner-dupe commentedThe module from #33 dont work for me.
Is changed anything in Location 6.x.3.1-code so thats module lost his function?
Regards Matthias
Comment #38
Nikki Aiuto commentedI completely agree with mandclu. Required location fields should not display the delete location check box.
Thankfully, the work around isn't painful and does the job.
Comment #39
gaëlgYep, #36 is right !
Comment #40
fabianx commentedHere is the code for 7.x (if someone needs it):
It does assume usage of Location CCK and the CCK field be named "field_location".
Best Wishes,
Fabian
Comment #41
ptmkenny commented#40 worked for me, though I had to delete the "custom_" from "custom_form_alter."
Thanks Fabian!
Comment #42
Gastonia commentedfor #40, is that a custom module? Is there a way to do this through the template.php file?
Comment #43
KorbenDallas commentedThanks for the code Fabianx, works great
Comment #44
spadxiii commentedSolution in #40 might work. But I've found that there is an alter-hook that makes this a bit easier. And cleaner imo.
Comment #45
fabianx commented#44: Nice :-)
Comment #46
zakangelle commentedCan confirm I dropped #44 in a custom module and it worked!
Comment #48
pdelorme commentedCool.
Thanks to #44 I finnaly created my first module and hook.
Working like a charm.
Comment #49
r0ber commented#44 works fine for me!. Thanks!
Comment #50
ann b commented#44 worked for me.
Comment #51
fruitsalad commentedtried to implement #44. Created a module folder a locationdelete.module file and a locationdelete.info file. Activated the module, but the field is still visible. Do I have to do more steps? Cleared the cache also.
locationdelete.module
locationdelete.info
Comment #52
dboulet commentedHi fruitsalad, you need to make sure to replace the prefix HOOK in the function name with the name of your module. So in your case, the function should be called
locationdelete_location_element_alter.