简体   繁体   中英

Custom Joomla landing page: Define articles, Contact form

In Joomla! you can change the code of the landing/default page with template overrides. While adding HTML elements and classes is an easy task, I can't figure out two things:

  1. add options to the menu item
  2. add a contact form

1. add options to the menu item

If you override the "featured" layout of the content component, basically you have a simple blog layout. I rather would like to define 5 articles that are used for different purposes on the page.
For example:

article_col1:        article-1
article_col2:        article-2
article_col3:        article-3
article_description: article-4
article_contact:     article-5

Can I do this and how?

edit 2015/09/03 :
I understand that I need to change the XML file describing the menu item type and its parameters. For now I copied the 'featured.xml' and added the request section from 'article.xml':

<fields name="request">
    <fieldset name="request"
        addfieldpath="/administrator/components/com_content/models/fields">

      <field name="id_contact" type="modal_article"
            label="COM_CONTENT_FIELD_SELECT_ARTICLE_LABEL"
            required="true"
            edit="true"
            clear="false"
            description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />

      <!-- and some more article fields -->
    </fieldset>
</fields>

Thus I can assign articles to the menu item now.
But how can I access and display them in the layout file? I can see that the intro articles are accessed via $this->intro_items . Unfortunately $this->id_contact->text doesn't work. Do I have to tell Joomla to load the articles from the database somewhere somehow?

2. add a contact form

If I can set, access and display these articles, I could use a plugin to show the contact form in one of the articles. However, I know that I want to show the contact form of a single contact at my landing page and a plugin for this very single case seems overkill to me.

Is there a way to include the form via PHP code?

As a preamble, I would like to point out a very small, dedicated component would be ideal for this solution. The reason being is template overrides are designed to change the way an existing model's data is present, so you have no control over what the application is going to deliver via $this->items.

And as commenter to my response pointed out, everything you are trying to do can be accomplished using featured articles and modules.

That being said :)

First thing is you should embed the custom request fieldset inside the existing container. In addition, since you want to select and display 5 different articles you would need a field for each one as well. So article.xml fields section would look like so:

<fields name="params">
    <fieldset name="request" label="Article"       addfieldpath="/administrator/components/com_content/models/fields">

        <field 
             name="article_1" 
             type="modal_article"
             label="Custom Article 1"
             required="true"
             edit="true"
             clear="false"
             description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />
        <field 
             name="article_2" 
             type="modal_article"
             label="Custom Article 2"
             required="true"
             edit="true"
             clear="false"
             description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />
        <field 
             name="article_3" 
             type="modal_article"
             label="Custom Article 3"
             required="true"
             edit="true"
             clear="false"
             description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />
        <field 
             name="article_4" 
             type="modal_article"
             label="Custom Article 4"
             required="true"
             edit="true"
             clear="false"
             description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />
        <field 
             name="article_5" 
             type="modal_article"
             label="Custom Article 5"
             required="true"
             edit="true"
             clear="false"
             description="COM_CONTENT_FIELD_SELECT_ARTICLE_DESC"
        />
    </fieldset>
</fields>

Now, to access the data from inside the article.php file use:

$menu = JFactory::getApplication()->getMenu()->getActive();
$article_1 = $menu->params->get('article_1');
$article_2 = $menu->params->get('article_2');
$article_3 = $menu->params->get('article_3');
$article_4 = $menu->params->get('article_4');
$article_5 = $menu->params->get('article_5');

Unfortunately, this will just give you the articles IDs. A quick and easy way to retrieve the articles would be to use the com_content table class like so:

$article = JTable::getInstance('content');
$article->load($article_1);

Now you could display article content using:

echo $this->escape($article->introtext);

Embedding the form is a bit more complicated, especially if you're only working with a template. My suggestion is to use one of the free extensions on JED (Joomla Extension Directory) to create the form and use one of their modules to display. Joomla has a built in Load Module plugin, that when activated allows you to embed modules inside other articles using:

{loadmodule mod_myform, My Custom Form Title}

Otherwise you would need to create your own module and xml form definition and a lot of other stuff which should honestly be its own question if you want to pursue this route.

Hope this helps.

I think you need to step back and understand a bit about how Joomla works. I'm not sure what you were using previously but I think you are making things too complicated. The point of using a CMS like Joomla is that for the most part you don't need to write custom queries. And you would never put a query in a template, that doesn't make sense. Template layouts in particular are for html and you only use PHP to say "echo this piece of content here" or sometimes to manage looping through content.

This is also a downside of the minimal sample data. If you are doing a complex site and don't know Joomla at all you should create an instance with the "learn Joomla" sample data and just mess around with the options until you understand how it works. That will show you all of the built in options and will also show you some more advanced ways to be creative with them in the fruit shop sample siate. The minimal sample data is really best for either experienced users, people who are going to make a very simple site, or people who are going to read a book or the docs or watch some videos about how to make a more complex site. Otherwise as I said you should install the complex sample data in a site to just mess with and delete when you are done.

So first of all, the concept of featured content is that you go to the article manager and select the articles you want to show in the featured view by marking them featured. So the first thing you should do is go and mark the articles you want to show on your homepage. Then go to the featured view in com_content and put them in the order you want and set any display options for the individual articles.

Then in your featured menu link that will also be marked as your default link, you should put the arrangement you want ie the number of columns you want the articles to be in, and you can have a mix of full width and multicolumn layouts.

Then the only issue is that you want to display an actual contact form as opposed to a link to a contact form on the home page, right? Most likely what I would do there is to make a module with the form. Probably you can find one of those in the extensions directory but you can code it yourself as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM