Drupal 8 hook_update_n code example needed - drupal

We have an existing custom module for our Drupal 8 application that created a content type and a view. The "news" module is in our production environment running fine; many pieces of content have been created using it.
Now enhancements have been requested: 2 new fields and a change to the view. I have made the changes using the admin UI in our dev environment, but I will need to put these changes into the module's code for version control so that in new site instances, one need not remember to make UI changes after installing the module.
The fields:
field_show_on_page, Field Type: List (text)
field_news_dateline, Field Type: Date
View:
The change in the view is on what field the sorting is done.
It would be simple to export the fields and view configurations and add them to the module>config >install folder, but for the change to be picked up, that requires uninstalling and reinstalling the module, which can't be done without needing to delete news content, which is not acceptable.
I understand the changes can be done in hook_update_N in the news.install file. But I cannot find examples for this function that is close enough for this need. I understand the function should be written as:
news_update_8001() {} but what would go in the body to add the two fields and update the view?
Thank you.

If you have the new fields in your config you can just delete the old fields by adding this in your hook and then run drush updb before you import the config:
$field = \Drupal::entityTypeManager()->getStorage('field_config')->load('node.news.old_field');
if ($field) {
$field->delete();
}

Related

New export action for pages in magnolia cms

I need to create a new export action for the page app in magnolia cms that would always export selected page elements to a YAML file.
I would like to override the class definition and the dialog definition for the existing export action since I do not need a dialog that lets me select YAML or XML. It will always be YAML in my case.
I setup a new Maven module and created a new action for the Page app.
How do I configure a custom class for this action? How do I get the current context of the page in my class?
You have to remove the dialog attached to the action first. If you plan to have a custom action then simply do not configure one. We already have two actions for those two cases. If you are interested in YAML export use the following: info.magnolia.ui.framework.action.ExportYamlAction
My action configuration for exportYAML2
My actionbar configuration for exportYAML
* repeating the content of the comment below since I could not post pictures in the comment *
I configured two actions for exporting the YAML configuration of the node.
The second one (exportYAML2) using ExportYamlActionDefinition does not show up in the actionbar for the page even though I added it as an item.
Is there anything else I need to configure for it?
exportYaml with class info.magnolia.ui.framework.action.OpenExportDialogActionDefinition works. exportYaml2 with class info.magnolia.ui.framework.action.ExportYamlActionDefinition doesn't work. Availability for it is set to info.magnolia.ui.framework.availability.IsNotDeletedRule.
It is solved.
There are two things I needed to configure for a custom action in the pages app in magnolia.
configuration app modules>pages>apps>pages>subApps>browser>actions>MyAction withe class={custom or common class from https://documentation.magnolia-cms.com/display/DOCS61/Action+definition }
modules>pages>apps>pages>subApps>browser>actionbar>sections>pageActions>groups>uniquegroupname>items-MyAction

Filter by Bundle Drupal 8 Link Autocomplete

Does anyone know how to limit the autocomplete suggestions of Link Fields to only certain bundles -- not globally, but per Link Field instance? I nearly found a solution in customizing https://github.com/minnur/Alter-Entity-Autocomplete , but this is global for all autocompletes, and I need to get the calling Link Field instance somehow.
This is possible with hook form alter:
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// if $form_id == 'xyz'
$form['field_mylink']['widget'][0]['uri']['#selection_handler']="default:node";
$form['field_mylink']['widget'][0]['uri']['#selection_settings']=[
'target_bundles'=>['article'=>'article', 'page'=>'page'], //etc
];
}
I have written the following Drupal contributed module to solve the issue:
Link Field Autocomplete Filter
The module adds a Link Field configuration for filtering the allowed
content types in the autocomplete field.
You can install the module manually with the drupal core Update Manager module (must be enabled) using the Drupal backend:
http://yourSite/admin/reports/updates/install
and follow the instructions there.
Using Drush, you can install it with:
drush dl link_field_autocomplete_filter; // download
drush en link_field_autocomplete_filter; // enable the module
You can also us Composer to download the module:
composer require drupal/link_field_autocomplete_filter
Simply enable the module, and a series of checkboxes (one for each content type) will appear in the Link Field field-instance configuration form. If you check none, then all content types will appear as suggestions in the autocomplete. Otherwise only those checked will appear.

Drupal views 2 API

I would like to create a custom views, because some database fields are not accessible by drupal views UI. I just exported an existing view into a file called my_module_views_default.inc that contains the hook_views_default_views() function. and by the drupal views2 document state that the hook gets called automatically, but it doesn't.
Also i want to know the path, we are giving the path in the code here
$handler->override_option('path', 'my_earnings');
That means we can see the views in the link http://localhost/drupal6/my_earnings ??
and
in my .module file, i use the hook_views_api as follows
function mymodule_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'my_earnings'),
);
}
I have found the document Using default views in your module and i created a new module, and i exporting a existing view. Then i placing my exported views into new module in file mymodule.views_default.inc. My views should now be listed as Overridden on the view list page and clear the Views cache. Revert these views, they will be removed from the database, but will remain in code.
Thanks alot to all..
I was caught with the same issue where i needed to join the two table in view. fortunately i found these links
http://drupalmodules.com/module/reverse-node-reference module enhances views with reverse relationships for node reference fields.
you may also need http://drupal.org/project/noderelationships
Using these module i don't think we need any custom module writing for view.

Rewrite default Drupal view programmatically

Say we have a defaul view (i.e hardcoded), provided by Views module , for example "taxonomy/term/%"
Now, I'd like to make some modification to that view programmatically, through an installation profile
Normally I use Features module for such work, but Features does not support default views.
Please advise how to do that.
Thanks!
Use hook_views_default_views_alter
function MODULE_views_default_views_alter(&$views) {
if (isset($views['taxonomy_term'])) {
$views['taxonomy_term']->set_display('default');
$views['taxonomy_term']->display_handler->set_option('title', 'Categories');
}
}
You should use the Views theme information. There is a link you can use to find out what you should name your views (Its called "Theme Information") copy the name of the particular part of the view you would like to hardcode and paste it as a new file in your template's directory. You can use a folder (I usually name it views) to separate these files from others in the template. You'll need to refresh your cache to see the changes once you've created the new template file(s).
Yes, use hook_views_default_views_alter()
Here's a good example:
enter link description here

How to quickly theme a view?

I've defined a view with the CCK and View 2 modules. I would like to quickly define a template specific to this view. Is there any tutorial or information on this? What are the files I need to modify?
Here are my findings: (Edited)
In fact, there are two ways to theme a view: the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".
with the "Node" way, you can create a node-contentname.tpl.php which will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']. (edit2) You can use node-view-viewname.tpl.php which will be only called for each node displayed from this view.
with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.
Thanks to previous responses, I've used the following tools :
In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
The Devel module's "Theme developer" to quickly find the field variable names.
View 2 documentation, especially the "Using Theme" page.
In fact there are two ways to theme a view : the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".
with the "Node" way, you can create a node-contentname.tpl.php wich will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']
with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.
Thanks to previous responses, I've used the following tools :
In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
The Devel module's "Theme developer" to quickly find the field variable names.
View 2 documentation, especially the "Using Theme" page.
A quick way to find the template files you can create and modify for a view in Views 2.0 is to:
Edit the view
Select the style (e.g. page, block, default)
In the 'Basic Settings' block click on 'Theme: Information' to see all the different templates you can modify.
The Devel module's "Theme developer" feature is handy for seeing what template files Drupal is looking for when it goes to theme something. See the screenshot on that page for an example.
You should also check out Semantic Views. For simple Views theming, it is really handy.
One tip:
You'll likely have a number of views which require similar formatting. Creating templates for each of these views and copying them creates a nightmare of code branching - if you're asked to change the whole look and feel of the site (implying changing the display of each of these views formatted in this particular way), you have to go back and edit each of these separately.
Instead of using the views interface to select new templates for views, I sometimes simply insert some code branching into a single views file. E.g. for one site in views-view-fields.tpl.php I have:
if($view->name == 'articleList' || $view->name == 'frontList'
|| $view->name == 'archiveList') {
/* field formatting code */
} else {
/* the default code running here */
}
This then modifies the fields in the way I want only for this family of Views = articleList, frontList and archiveList - and for other views using this template runs the code one normally finds in this template. If the client asks, "Hey, could you make those pages showing the archives & that list on the front page to look more like ( ... )", it's simply a matter of my opening & editing this one file, instead of three different files. Maintenance becomes much more quick & friendly.
for me block-views-myViewName-myBlockId.tpl.php works
My shortcut option.
Go to theme.inc file in YOUR_MODULE_DIR/views/theme/ folder.
In the _views_theme_functions function print the $themes variable or put a breakpoint on the last line of the function to see the content of the variable.
Just convert views_view to views-view and __ to -- and add your template extension to get desired file name.
For example if an element of the $themes array is views_view__test_view__block (where test_view is the name of your view) then the name of the template file would be views-view--test_view--block.tpl.php.
In my opinion the simplest way to decide which template file to use for theming the views is :
1) Click on admin/build/views/edit/ViewName -> Basic Settings -> Theme
Clicking this would list all the possible template files. Highlighted (File names in Bold) files indicate which template file is being used to do theme what part of the view. After incorporating the required changes in the relevant view template file RESCAN .. now you should be able to see the changed template file highlighted .
If you want to do quick Drupal development with a lot of drag-and-drop, the Display Suite module def. is a something you should use: http://drupal.org/project/ds
According to me there are two ways to do it:
Programatic Way:
Go to edit view.
Select page/block style.
Go to 'Basic Settings' and click on 'Theme: Information' to see all the different templates you can modify.
Add the html you want to theme and print the variables of the view wherever needed
Configuration Update: The Display suite provides us an option to place your labels inline or above and add even to hide them. Custom classes to each of the view's elements can be added too.
Advanced options include:
Exportables
Add your own custom fields in the backend or in your code
Add custom layouts in your theme (D7 only)
Change labels, add styles or override field settings (semantic fields).
Full integration with Views and Panels
Extend the power of your layouts by installing Field Group
Optimal performance with Object cache (D6) or Entity cache (D7) integration

Resources