Symfony 2 Nav. Menu, open in new window - symfony

I'm attempting to create a new window when a user clicks a link in the navigation menu. What I'm attempting to do at present is just add a target="_blank" item to the url, thereby creating an entirely new page, and from there I'm planning to learn how to change this for my various other needs. The problem is, I'm unable to get the target to go to the associated link.
I've attempted:
$dropdown->addChild('Text', array('route' =>
'routeName', 'routeParameters' => array('parmName' => 'parameter'),
'attr' => array('target' => '_blank'));
But the above results in it not adding the _blank to anything.
$dropdown->addChild('Text', array('route' => 'routeName',
'routeParameters' => array('paramName' => 'parameter')))
->setAttribute('target', '_blank');
Results in the target being set to the li, not to the link itself, as seen below.
<li target="_blank" class="first"> Text
Is there a means to directly set the attribute to the link, so that it will open in a new window when clicked?
Any time and help you can provide is much appreciated.

For what's worth, if you want to do that at the addChild method:
$menu->addChild('Homepage', [
'route' => 'homepage',
'linkAttributes' => ['target' => '_blank'],
]);

If you are using KnpMenuBundle (it sure looks like it)
you can do it like this:
$dropdown->setLinkAttributes(array('target' => '_blank'));

Related

WordPress - Fully Customize Admin Bar?

Is there a straightforward way to fully customize the backend WP admin (top) bar? I want to replicate the frontend nav with logo/styling and a few links. I've googled this to no end and haven't found any clear cut way to "create your own" admin bar (avoiding plugins).
My customers access all of their paid content on the backend of WP, so I'd like to mimic the branding/logo/style/links of the frontend. Thank you to any helpful answers/resources!
You can use the admin_bar_menu action to customize the default admin bar by removing/modifying existing menus and adding new ones. You can also add sub-menus too.
It would take a bit of work to completely overhaul it, but here is the basic syntax for creating and adding menus to it (place in functions.php or equivalent):
add_action('admin_bar_menu', 'nebula_admin_bar_menus', 800);
function nebula_admin_bar_menus($wp_admin_bar){
$wp_admin_bar->add_node(array(
'id' => 'nebula-github',
'title' => 'Nebula Github',
'href' => 'https://github.com/chrisblakley/Nebula',
'meta' => array('target' => '_blank')
));
$wp_admin_bar->add_node(array(
'parent' => 'nebula-github',
'id' => 'nebula-github-issues',
'title' => 'Issues',
'href' => 'https://github.com/chrisblakley/Nebula/issues',
'meta' => array('onclick' => 'exampleFunction(); return false;') //JavaScript function trigger just as an example.
));
}
This method could be done in a loop to mimic your frontend nav. HTML can be included in this and styles can also be applied to the elements as needed. You can even run JavaScript functions on click events if you wanted to (example in snippet above).
I hope this is in the ballpark of what you're looking to do, or at least gets you headed in the right direction.

Get out an error message if noone of checkboxes selected

I have set a checkboxes form that works perfect, i have this if statement which also works perfect but what i really want is when the user hasn't selectected any checkbox from the list, when he pushes the save button to get out a drupal error that will says "Oh!you do not select nothing"... How i can do this thing?
if (!$selected) {
drupal_set_message(t('You have to select at least one option from the list.'), 'warning');
}
You can set the message manually from your code:
https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_set_message/7
Where you have that your if logic add drupal_set_message() call.
Do you use Form API?
If yes try the #required property
$form['test'] = array(
'#type' => 'checkboxes',
'#options' => $myOptions,
'#required' => TRUE,
'#title' => t('Test'),
)

Autocomplete without using a callback

I want to use autocomplete-fields that link to an external source for their autocomplete-data. Drupal seems to refuse all autocomplete_paths that are not reachable within Drupal. Any ideas how to circumvent that problem? The form field looks like that:
$form['business_city'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('city'),
'#autocomplete_path' => '_/city?=',
'#default_value' => $userProfile->field_address_business_city[0]['value'],
);
_/city is not reachable within Drupal for performance reasons. The script bootstraps Drupal up to session-level to check for a valid login.
UPDATE:
If I create an autcomplete-field by attaching the needed markup manually to the field it works but it is awkward to maintain:
'#attributes' => array('class' => 'form-autocomplete'),
'#suffix' => '<input type="hidden" disabled="disabled" value="/_/city?n=" id="edit-private-city-autocomplete" class="autocomplete">',
Instead of hacking, you could make sure that the path you are querying "/_/city?n=" is a valid menu_hook item. That way it will validate against the drupal_valid_path() inside theme_textfield(). From within the menu hook function callback you could then forward the request to your external data source.
Drupal 6 validates in theme_textfield() if the autocomplete path is a valid (internal) path.
So, you can't work around this unless you override that theme function.

Create menu link to view with argument

I'm trying to use hook_menu to create a link to a view which takes an argument. However if I use the path (in $items[view-path/%dest]) that I've already set as the path in the view then the link doesn't appear. I'm guessing there's a path conflict somewhere. Is there a way round this? Or can I use another method to return the view?
I'm using the following code:
/**
* implementation of hook_menu().
*/
function sign_custom_menu() {
$items['view-path/%dest'] = array(
'title' => 'Link to view',
'page callback' => 'sign_custom_hello',
'page arguments' => array(1), //(corrected typo from 'page arguements')
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'menu-student-links',
);
return $items;
}
function dest_to_arg() {
// would normally be dynamic to get view with correct argument
$arg = 73;
return $arg;
}
Thanks in advance.
Addition
function sign_custom_hello() {
//return t('Hello!');
}
I managed to answer my problem. Basically I used a different path to the one I had set in the view and then used views_page() as my "page callback". I passed it the arguments for the view, the page ID and it's own additional arguments to make the view work. I was able to use a wildcard in the menu item to pass to views_page() by using the to_arg() function that works with hook_menu() to pass in wildcards. The 'page arguments' pass in the three arguments. The last argument, "1" is a reference to which position in the path the argument appears (starting from 0).
The working code is:
<?php
/**
* implementation of hook_menu().
*/
function sign_custom_menu() {
$items['view-path/%dest'] = array(
'title' => 'link to view',
'page callback' => 'views_page',
'page arguments' => array('view_name', 'page_1', 1),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'menu-student-links',
);
return $items;
}
//this function is needed from the "%dest" argument in hook_menu above
function dest_to_arg() {
// would normally be dynamic to get view with correct argument
$arg = 73;
return $arg;
}
?>
I don't have much experience with wildcard URLs in my custom modules much, but I researched the issue in the Pro Drupal Development book. From what I read in the "Wildcards and Parameter Replacement" section on page 77, I think you may want to use $items['view-path/%'] instead. Using %dest apparently makes drupal look for a dest_load function.
Items that appear in menus can't be created by a wildcard router item: each menu item corresponds to exactly one path. That is, if you have a router item that is foo/%bar and %bar can have 10 different values, Drupal's menu system isn't going to create 10 new menu items off of one router item definition.
So what you're going to need to do is create a router item for each possible argument ahead of time. Otherwise, you're going to have to look outside Drupal's menu system and think about creating a separate Views block that looks like a menu but is really a Views unordered list of the available options.
To do the former, you need to implement hook_menu_alter() to add your custom router item after everything else, including the wildcard router item you're trying to override. Your custom router item will be more or less the same as the wildcard router item, but with some defaults set that would normally be derived from the wildcard.
For example, if I wanted to create a new router item for user/1/edit which overrides the built-in user/%user_category/edit, I'd implement hook_menu_alter() as so:
function mymodule_menu_alter(&$items) {
// user_edit and user_edit_access expect a user object
$account = user_load(array('uid' => 1));
$items['user/1/edit'] = array(
'type' => MENU_CALLBACK,
'page arguments' => array($account),
'access arguments' => array($account),
) + $items['user/%user_category/edit'];
}
In this example, user/%user_category/edit calls user_edit() and user_edit_access() for the page and access callbacks, respectively, and they both attempt to use the wildcard. Since there is no wildcard in your router item, you need override the arguments to say "check user 1".
You'll do this for each and every possible value of the wildcard.
But this isn't enough: notice I used MENU_CALLBACK instead of MENU_NORMAL_ITEM. If you use MENU_NORMAL_ITEM, your router item is going to show up in the Navigation menu, not in your custom menu, even if you set menu_name (I don't know why this is: it should work). But you can get around this by using menu_link_save().
Consider this implementation of hook_init():
function mymodule_init() {
$router_path = 'user/1/edit';
// Check to see if the custom router item has been added to menu_links.
// This is to ensure the menu has already been rebuilt.
$router_item = db_fetch_object(db_query("SELECT * FROM {menu_links} WHERE router_path = '%s'", $router_path));
// Only create a new menu item if the router item exists and it
// hasn't already been created (it's hidden until created).
if ($router_item && $router_item->hidden) {
$item = array(
'link_title' => 'Edit Administrator',
'link_path' => $router_path,
'menu_name' => 'primary-links',
'router_path' => $router_path,
'mlid' => $router_item->mlid,
);
// Save the menu item.
menu_link_save($item);
}
}
In this implementation, it checks to see if the custom router has already been created and hasn't been otherwise modified. If this is true, it creates a menu link in the primary links menu that references your custom router item.
Obviously, since this is in hook_init(), it'll perform the check on every page: this is to ensure it fires after the menu is rebuilt. It shouldn't be much of a performance hit, but it's something to keep in mind.
As you can see, it's a long and drawn out process to do this: if you're not going to go the Views fake-menu route, it might be better to just manually create the links yourself.

including javascript function in .module file in drupal

on click of button i want hi message to be displayed using javascript in drupal.I have made a .js file and know that to incude that i must use drupal_add_js(drupal_get_path('module', 'document') .'/click.js'); but the problem is to create button i used $form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>drupal_add_js(drupal_get_path('module', 'document') . '/cancel.js')),
'#value' => t('click'),
);
I want that hi message which i have included in js file to be shown when button is clicked.
Please help
Hi thanx for your concern..........
here is the way i proceeded in .module file
function document_form(&$node) {
$form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>message()),
'#value' => t('click'),
);
}
function document_form_alter(&$form, &$form_state, $form_id) {
drupal_add_js(drupal_get_path('module', 'document').'/cancel.js', 'module');
$settings['click'] = array(
'nid' => $form['nid']['#value'],
'cid' => $form['cid']['#value'],
'uid' => $form['uid']['#value'],
'pid' => $form['pid']['#value'],
);
drupal_add_js($settings, 'setting');
}
and my .js file code is as follows:
function message()
{
alert("This alert box was called");
}
<body>
</body>
but still onclick of button i m not getting the message "This alert box was called"
Kindly help where the problem is coming now.......
Thanx in advance....
in wait of your response
The form alter won't add the JS file in the way you are wanting.
In the function you create the form you can use drupal_add_js, outside of the creation of the form array.
Then you can use the onclick to call the function in your JS file.
A better way to do this is to use drupal behaviours to add an click listner to the button (see example here).
Looks like simplest solution would be, as you actually don't need the button to go to submit and other form stuff.
add a link to the text
style the the link as button using css .mybuttons{}
hook the js on the id. $(#mybutton1).alert..

Resources