Block positioning using admin section - php

In my admin section i have a block maker.
The block is very simple, block_title, block_content.
What is the simplest way to show / determine a themes layout in the admin section so the user can position the blocks?
One idea is to use numbers so, 1 = block area one, 2 = block area two etc
A function in the front end could look like this:
function showBlocksForArea(1) {
// Return blocks Array where block_area=1
}
Rendered in the theme / template as:
echo'<div id="block_area_one">';
foreach($view->showBlocksForArea(1) as $row) {
echo '
<div class="block_wrap">
<div class="block_title">'.$row['block_title'].'</div>
<div class="block_body">'.$row['block_body'].'</div>
</div>
';
}
echo '</div>';
This is fine, accept the user doesn't know without looking at the theme where those areas are.
The Admin Section
Something like this would be perfect if i could grab just the HTML of the layout:
Is there a simpler way?

The following is an answer based on the chat that occurred here: https://chat.stackexchange.com/rooms/18020/room-for-xdaevax-and-codex
Here is my understanding of your goals:
You are creating a type of CMS application used to create websites
The websites are modular and will allow administrators to easily customize both the look and feel of their website and the content, without having extensive code knowledge
To facilitate this, you've created a system of templates, and content, that can be combined in various ways to create these themed pages. This is driven primarily by a set of PHP functions that extract the template and block data from the DB.
As new themes are created, the admin is able to "just work" without any modification because the functions can pull in any theme and it can be combined with any template.
The templates will have to have a naming structure that is generic so that CSS can be easily swapped out.
Given this information, I would suggets doing the following:
Database Schema
I would set up a database structure to something that resembles this schema I made:
The idea here is that a Template record can have any number of "slots". The DB is agnostic in terms of where the slots go on the page (this is the responsibility of the theme), but the TemplateSlots are used to provide "place holders" for where content (Blocks) can be placed.
Each time a user creates a new Page, a Page record is added and the page is associated with a Template, from which it also has information about the slots. The Template also defines a file (a PHP file most likely) that specifies place holders where the slots are placed. The Page gets the Theme information from the Site which it is associated with. Each Theme can have a set of ThemeContents (multiple CSS files, js files, images, etc....).
Blocks can be created that could have content assigned to them. These blocks are independent of a Template initially, and the DB could be pre-populated with common building 'blocks'. These blocks can be associated with a Template and assigned a TemplateSlot once a block is assigned to a Page by having a PageBlock record created.
DB Conclusion
This structure allows a great deal of flexibility and allows the Database to handle many potentially complex user scenarios. By segmenting the Template and Slots into separate tables, you can dynamically create templates of all shapes and sizes without having to make any code changes.
PHP Admin
For your admin, you can use whatever mechanism you like to pull the data out of the DB. But the user workflow would look something like this:
Log in to admin
Select Operation (a-> Create New, b-> Update Existing)
Select Page Template
Assign content blocks to template slots
Preview
Compile changes (a -> Save New Page, b-> Update Existing)

Related

What is the best way to create page in Drupal 8 so that I can then add these posts with images?

I have a Home page on the Drupal website (such as is created after installation), but I still need to create this page:
What is the best way to create a page so that I can then add these posts with images?
I am just starting to learn Drupal and have heard so far about such ways of creating pages:
1) in admin toolbar: Content / Add Content / Article
2) in admin toolbar: Content / Add Content / Basic page
3) in admin toolbar: Structure / Views / Add Views
Which one should I use? Or maybe there is some other option that I don’t know about?
P.S. At the moment I am more interested how to create empty page on which I can then add posts later, and adding posts it is another question.
Welcome to Drupal.
Drupal ships with the default theme which won't look nice but it does its job in the right way. Now if you want to create a better UI/UX obviously you should create a new theme. But before that make sure to read and understand the concepts behind Drupal. Drupal docs are your first friend.
Drupal Documentation
Drupal considers everything as nodes and that's how Drupal got its power. As you mentioned, Articles, Basic Page etc are called content types and they can be used to create a particular type of content.
Now for your purpose create a new content type and add the fields you need. From the image above I can say your content type needs Title, Image, Category and Date. After creating content type you can create as many contents as you want under the content type you just created. Consider each card in your image as content.
Now you can use a Drupal Core Module Views, to perform DataBase Operations without writing single code. Yes, you can select fields, sort, order etc with Views UI and display it in a page or a part of a page (Block).
I would say just try this out in the default Drupal theme and when you understand how this works, you can start creating your own theme for your project.
Theming Drupal
There is a lot of resources available. But you have to make sure what you are asking is whether you actually need. It will take some time, but it worth.
To build layouts for homepages on Drupal 8 you best friend is https://www.drupal.org/docs/8/core/modules/layout-builder
To build the content blocks inside your home page, you should start creating nodes on a node content type to hold your information. For instance: news content type, with a title, a body, a date, and an image.
For every node type, ex. news, work on the preview display, full display, and any other display which makes sense. These displays can be used later in the Layout Builder directly or in Views, referenced below.
If you wish your list to be dynamic, such as the last 10 entries are shown first, then use a view to hold the content sorted and filtered as you need.
In a nutshell.
Create a content type for your article/news.
Modify the displays of the content types to have at least a summary and full view.
Create the content itself to have something to see.
Create the view (block) to filter and sort your content.
Create a page layout (this makes sense for landing pages) which places your new view and any other content you need in any disposition.
This is roughly what I would do. The steps described above contain many intermediate steps. If in doubt, check the docs.
I hope that helps!
First, decide what will you display on that page. Is it content in some existing content type (article maybe) or you want to crate new one for this purpose.
If you need new one then create it (Structure -> Content types -> Add content type).
Then check what fields will you need. I.e. image, some description text. Add missing ones.
Create few nodes (pages) in that type so you could work with them.
Then for displaying you should crate a view (Structure -> Views -> Add view). It can be a page view (you are displaying only that content on page) or block view (this is just a block among some others). If you create a page you could visit it and if you create a block you have to add it to some region to appear on page (Structure -> Block Layout).
Inside your theme you should create templates for this page/block. Turn on twig debug mode so it will show you hints - what templates are used and how can you name yours to override default ones.
Adjust CSS to make it look like you want it to look.
Find some tutorial(s) for the details
Previous answers have given the flow of the work you should go through, I would like to add some resource that might help you achieve this.
Creating content type and fields: https://www.drupal.org/docs/administering-a-drupal-site/managing-content-0/working-with-content-types-and-fields
https://www.drupal.org/docs/user_guide/en/structure-content-type.html
View and View modes: https://www.drupal.org/docs/user_guide/en/views-concept.html
https://www.drupal.org/docs/8/api/entity-api/display-modes-view-modes-and-form-modes
Handling block of the view: https://www.drupal.org/docs/8/core/modules/block/overview
Feel free to ask if any further explanation is needed.
Thank you

How does WordPress Dynamically Generate Pages?

I'm new to web building and programming and what not, but I'm trying to create a website with a lot of pages. Note that this is pages not posts. The pages will use the same layout, but the content will be different. It's very inefficient to create the pages in Wordpress. I was wondering where does Wordpress store the database that is used to generate the pages. I figured I'll just add information to the data base - which would be much for efficient.
Any ideas on how to do this? Or other ways to create many pages efficiently. I would love to be able to work with Excel or some other text software, rather than the slow Wordpress platform.
Thanks!
All content are saved to the wp_posts table in your WordPress database. whether its for pages or as posts. Databases are provided by your webhost. Edit: Excel is not a web authoring program, nor should it be used as one.
You should put your header in header.php and footer in footer.php, and main content if it's not post type in page.php or you can make you custom page template, also if you are use page and not post, don't forget change it from settings
You can't use Excel for your data. You can only use the Wordpress Database or external files.
As stated above, pages are stored in the posts table.
Since your data isn't actually dynamic, you should probably go ahead and create individual pages and create a template to display the pages the way that you want them to look.
https://developer.wordpress.org/themes/template-files-section/page-template-files/
For the record, you can use a single page to output different content. Most shopping carts, for example, are displayed using a page called 'Cart' (or something similar), but the output is different. The Events Manager plugin outputs events to a page called 'Events'. This, however, is for truly dynamic data and not what you are looking for.

Drupal 7: Custom 'Node Submit' Page through Views?

I'm a designer (read: I know Photoshop) with HTML/CSS skills, and limited PHP skills. I've done some Drupal templates, I've worked with views, panels, etc. So I can't create modules.
Can I still use the Form API (inside views, through rewriting field output for example) to create a custom "Submit Node" page?
If so, how would one accomplish this?
I believe it requires much coding to achieve that.
To get yourself started in rewriting views output, go to your views page "admin/structure/views/view/VIEW_ID/edit" -> Advanced section -> Theme: Information at the very bottom of the list.
Display output section: all the template suggestions that can be applied to the whole view (only one template can be applied).
Style output section: any template in this section is applied to every row of the view result.
Row style output section: any template in this section is applied to every field in each row of the view result.
Then you will find template suggestions to every field in the view.
http://views-help.doc.logrus.com/sites/all/modules/views/help/images/style-breakdown-large.png

Drupal *_preprocess with a module

I'm new to Drupal dev, and was trying to add an existing region variable to my module's preprocessor function.
Basically, I've created a new region for my site (here's the definition from within side my theme's .info file):
regions[feeds] = Feeds
From Administer->Blocks, I've added the blocks I want to the new "Feeds" region.
Then, from another module, the "Advanced Front Page" module, I'm trying to add some PHP to my "front page" inside this module. The Advanced Front Page module just allows the site to have a landing page, instead of immediately viewing a list of other site content, etc. I've enabled PHP for the content area, and then added the following:
<div>
<?php print $feeds; ?>
</div>
It does not print the "Feeds" region, and I believe it's because that region variable is not accessible from outside of the page.tpl.php file. So after looking around, I came upon these links:
http://drupal.org/node/223430
http://drupal.org/node/237391
From there, I tried to add a preprocessor function for the module "Advanced Front Page", which has a module name of "front_page" (or possibly just "front", I'm not 100% sure). Here's my preprocessor function, that I tried to add to both my template.php file, and the /modules/front/front_page.module file (not at the same time, mind you):
function front_preprocess(&$vars)
{
$vars['feeds'] = theme('blocks', 'feeds');
}
Regardless of where I've placed this file (template.php or front_page.module) it doesn't seem to do anything. Any idea where I might be going wrong?
There are several points to address in your question:
I'd second googletorps answer in that you should approach this in a different way.
The *_preprocess functions can only be used to manipulate/inject variables for templates or theme functions, e.g. page.tpl.php, node.tpl.php, theme_links(), etc. As the front_page module does not use a theme function or (special) template to render its output, you can not make the $feed variable there by means of a *_preprocess function.
Sidenote: With *_preprocess functions, naming is crucial. You need to prefix the function name either with the exact module name or the theme name, depending on where you declare it. So in your example, if you want to add a preprocess function to the module, you'd prefix it with 'front_page_', if you add it to your themes template.php, you'd add 'yourThemeName_'.
You could achieve what you want by creating the blocks directly from code in your frontpage content area. Instead of trying to output the (not available) $feed variable, you could call:
theme('blocks', 'feeds')
This will cause Drupal to return the themed blocks for the given region ('feeds' in this case). Note that this is still not a good way to do it, as even if you don't use the region in your page.tpl.php, it still gets created for every page request made to your site!
So I would go with googletorps suggestion, adding the new region only if there are other uses for it also. If I'd just wanted to add some blocks to the frontpage, I would not create a new region, but configure the blocks to show in the content region and simply restrict them to show only for <front> in their visibility settings.
I haven't tried the advanced front page module, but when dealing with regions, you shouldn't do what you have done. It's a bit hacky and actually not needed. I don't know how the module hook into the templating system, but your problem is probably getting region variables into it's scope. But instead of trying to get the region into the frontpage using the module, you should instead get it into your page.tpl.php. You can actually do what you first tried, but I would suggest that you alter it a bit like this:
<?php if ($feeds): ?>
<div id="feeds">
<?php print $feeds; ?>
</div>
<?php endif; ?>
I have improved in two ways.
By adding the if statement, you don't add empty markup. So you wont get an empty div if $feeds doesn't contain anything.
Adding id's to regions is a good idea. It makes styling them or their content a lot easier, also it adds semantic to your page which html is all about.
Now if you only want your blocks to be shown in the front page you can set that up in each of the blocks settings. So you could possibly just use a region that already exist, unless you want your blocks display a outside an existing region. When adding regions it's not a good practice to only add a region to a single page, instead it's much better to control when it's content should be shown. It might be that you don't need to create a new region, but simply can use one that's already made. Also if you want to make some template changes to your front page, you can also create the front-page.tpl.php where you can create a different template layout for your front page if you so desire.

Drupal best practices for custom admin section

Is this a common task in your Drupal work when you're asked to create a custom admin section in Drupal for your content?
For instance, you have a few content types and user wants not only to view them, but to search using different filters, to add new items - all inside the same admin subsection.
In essence, this is classic search/grid/record screen/pages and while in typical Drupal distribution content management screents are kind of scattered around, I am talking about more organized spage, e.g. in which Grid (View) also has "Add new" link and you don't have to scroll to "Create content" menu which relates to a different section.
What is your experience and opinion in this relation?
I'm actually working on something like this right now where I work. We've built a custom module to solve Drupal's biggest problem of having unorganized content by organizing it in a nice tree structure. What we've decided to do is go ahead and create a whole new section within the admin just for managing content through this module (it contains a lot more functionality than just a tree structure). There were two big deciding factors when deciding to do this.
We knew we weren't going to be releasing this module so we didn't need to worry about integration with other modules because any such integration would be done by us.
The non-technical staff that this was presented to preferred this method over having several different admin pages with different pieces of functionality on each one, despite all the functionality being related. They viewed at as simply being easier to use.
The point behind my explanation is this: if it makes sense in your situation, then I'd say make either a new section or a new page with several tabs on it. There's a lot of deciding factors to consider:
Do I (or my) clients like the Drupal admin as is?
Do I need to worry about integration with third party modules?
Is time of the essence (this one is a big one)?
How extensible do I need the new admin page(s) to be?
Is upgrading to the newest version of Drupal of big importance (from 5 to 6 and later from 6 to 7 and so on)?
Many non-developers I've talked to abhor the Drupal admin and most content editors I've talked to or worked with prefer having a single admin page they can go to to manage all of their content. As a result, our setup is well received by our clients even though some within the Drupal community may not agree with it.
you can use views_bulk_operation module to show content (or users) to execute (batch) operations on them. in this way you get a sortable grid with search capabilities (exposed filters), pagination and the possibility to execute custom bulk actions. from here it's easy to add a link "create new" in the view footer. furthermore, you can use link as "local tasks" to organize views in tabs.
usually i use simplemenu for the admin backend and i hide the navigation menu in the sidebar for all but administrators.
Another option - much easier to deliver - would be to create a new menu only visible to the administrators - and dependent on the content displayed - with links to the pages for common tasks. Particular views, for example, or to add new content of the currently displayed type, etc.

Categories