Probably not the best title, but I've created a content type and don't want it to display anything to the public. It needs to be published and to have an 'edit' page but I don't need/want it to be viewed by the public or search engines. For example, let's say I created a content type called 'slide' and I was planning to use 'views' to create a block with a carousel to generate a slider. This can be expanded with a 'taxonomy' (terms like Home, About, Contact) allowing 'slides' to specify which pages they appear on.
The content type 'slide' doesn't need a viewable page it's just a building mechanic to make it easy for the user/client to update the sliders on the site.
'slide' is a good example but 'cta' is another example, or even 'team' where individual team members don't need their own page as they're displayed on the main team page.
The solution is use THEME_preprocess_page() to redirect to the edit page if this content type is viewed. If the user is logged out, they'll be prompted to login.
If my theme was called myD9theme then the following would be in the myD9theme.theme file.
function myD9theme_preprocess_page(&$variables) { $ctList = array('slide', 'cta', 'team'); // array of content types if( isset($nodeType) && isset($nodeId) && in_array($nodeType, $ctList) ) { $response = new RedirectResponse('/node/'.$nodeId.'/edit'); $response->send(); } }
Remember to rebuild the cache or your new code won't be read!