Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

013. Adding Published Unpublished

Astrid edited this page Oct 17, 2019 · 3 revisions

In this chapter we will ...

Now, we want to use publish and unpublish.

t13_1

Newly created or Modified files

Newly created files

administrator/components/com_foos/Controller/FoosController.php

administrator/components/com_foos/sql/updates/mysql/1.13.0.sql

Modified files

changelog.xml

foo_update.xml

administrator/components/com_foos/Table/FooTable.php

administrator/components/com_foos/foos.xml

administrator/components/com_foos/forms/foo.xml

administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini

administrator/components/com_foos/tmpl/foo/edit.php

administrator/components/com_foos/tmpl/foos/default.php

All changes at a glance

Click here to see all changes compared to the last chapter

More detailed explanations

File Structure

Example in Joomla 4

Side Note

Test your component

Now you can zip all files and install them via Joomla Extension Manager. After that you can see a link to your component in the left side menu. Clicking on this link will open the basic backend view.

Concluding Remark

Now we have . Up to now we have no . We are going to work on this in the next chapter.

Overview of all files

== Requirements == You need Joomla! 4.x for this tutorial (as of writing currently Joomla! 4.0.0-alpha11-dev)

== File Structure ==

=== Creating administrator/components/com_foos/Controller/FoosController.php ===

The administrator/components/com_foos/Controller/FoosController.php file is ....

==== Completed administrator/components/com_foos/Controller/FoosController.php file ====

The code for the administrator/components/com_foos/Controller/FoosController.php file is as follows:

namespace Joomla\Component\Foos\Administrator\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CmsApplication; use Joomla\CMS\MVC\Controller\AdminController; use Joomla\CMS\MVC\Factory\MVCFactoryInterface;

/**

  • Contacts list controller class.

  • @since 1.0 / class FoosController extends AdminController { /*

    • The prefix to use with controller messages.
    • @var string
    • @since 1.0 */ protected $text_prefix = 'COM_FOOS_FOOS';

    /**

    • Constructor.
    • @param array $config An optional associative array of configuration settings.
    • Recognized key values include 'name', 'default_task', 'model_path', and
    • 'view_path' (this list is not meant to be comprehensive).
    • @param MVCFactoryInterface $factory The factory.
    • @param CMSApplication $app The JApplication for the dispatcher
    • @param \JInput $input Input
    • @since 1.0 */ public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null) { parent::__construct($config, $factory, $app, $input);

    }

    /**

    • Proxy for getModel.
    • @param string $name The name of the model.
    • @param string $prefix The prefix for the PHP class name.
    • @param array $config Array of configuration parameters.
    • @return \Joomla\CMS\MVC\Model\BaseDatabaseModel
    • @since 1.0 */ public function getModel($name = 'Foo', $prefix = 'Administrator', $config = array('ignore_request' => true)) { return parent::getModel($name, $prefix, $config); } }

=== Changing administrator/components/com_foos/Model/FoosModel.php ===

The administrator/components/com_foos/Model/FoosModel.php file is ....

==== Completed administrator/components/com_foos/Model/FoosModel.php file ====

The code for the administrator/components/com_foos/Model/FoosModel.php file is as follows:

namespace Joomla\Component\Foos\Administrator\Model;

defined('_JEXEC') or die;

use Joomla\CMS\MVC\Model\ListModel;

/**

  • Methods supporting a list of foo records.
  • @since 1.0 / class FoosModel extends ListModel { /*
    • Constructor.

    • @param array $config An optional associative array of configuration settings.

    • @see \JControllerLegacy

    • @since 1.0 / public function __construct($config = array()) { parent::__construct($config); } /*

    • Build an SQL query to load the list data.

    • @return \JDatabaseQuery

    • @since 1.0 */ protected function getListQuery() { // Create a new query object. $db = $this->getDbo(); $query = $db->getQuery(true);

      // Select the required fields from the table. $query->select( $db->quoteName( explode( ', ', $this->getState( 'list.select', 'a.id, a.name, a.catid' . ', a.access' . ', a.published' . ', a.publish_up, a.publish_down' ) ) ) );

      $query->from($db->quoteName('#__foos_details', 'a'));

      // Join over the asset groups. $query->select($db->quoteName('ag.title', 'access_level')) ->join( 'LEFT', $db->quoteName('#__viewlevels', 'ag') . ' ON ' . $db->quoteName('ag.id') . ' = ' . $db->quoteName('a.access') );

      // Join over the categories. $query->select($db->quoteName('c.title', 'category_title')) ->join( 'LEFT', $db->quoteName('#__categories', 'c') . ' ON ' . $db->quoteName('c.id') . ' = ' . $db->quoteName('a.catid') );

      return $query; } }

=== Changing administrator/components/com_foos/Table/FooTable.php ===

The administrator/components/com_foos/Table/FooTable.php file is ....

==== Completed administrator/components/com_foos/Table/FooTable.php file ====

The code for the administrator/components/com_foos/Table/FooTable.php file is as follows:

	<field
		name="name"
		type="text"
		validate="Letter"
		class="validate-letter"
		label="COM_FOOS_FIELD_NAME_LABEL"
		size="40"
		required="true"
	 />

	<field
		name="published"
		type="list"
		label="JSTATUS"
		default="1"
		id="published"
		class="custom-select-color-state"
		size="1"
		>
		<option value="1">JPUBLISHED</option>
		<option value="0">JUNPUBLISHED</option>
		<option value="2">JARCHIVED</option>
		<option value="-2">JTRASHED</option>
	</field>

	<field
		name="publish_up"
		type="calendar"
		label="COM_FOOS_FIELD_PUBLISH_UP_LABEL"
		translateformat="true"
		showtime="true"
		size="22"
		filter="user_utc"
	/>

	<field
		name="publish_down"
		type="calendar"
		label="COM_FOOS_FIELD_PUBLISH_DOWN_LABEL"
		translateformat="true"
		showtime="true"
		size="22"
		filter="user_utc"
	/>

	<field
		name="catid"
		type="categoryedit"
		label="JCATEGORY"
		extension="com_foos"
		addfieldprefix="Joomla\Component\Categories\Administrator\Field"
		required="true"
		default=""
	/>

	<field
		name="access"
		type="accesslevel"
		label="JFIELD_ACCESS_LABEL"
		size="1"
	/>
</fieldset>

=== Changing administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini ===

The administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini file translation file. In this part you can learn how to use this with plural.

==== Completed administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini file ====

The code for the administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini file is as follows:

COM_FOOS="[PROJECT_NAME]" COM_FOOS_CONFIGURATION="Foo Options"

COM_FOOS_MANAGER_FOO_NEW="New" COM_FOOS_MANAGER_FOO_EDIT="Edit" COM_FOOS_MANAGER_FOOS="Foo Manager"

COM_FOOS_TABLE_TABLEHEAD_NAME="Name" COM_FOOS_TABLE_TABLEHEAD_ID="ID" COM_FOOS_ERROR_FOO_NOT_FOUND="Foo not found"

COM_FOOS_FIELD_NAME_LABEL="Name"

COM_FOOS_FIELD_FOO_SHOW_CATEGORY_LABEL="Show name label" COM_FOOS_FIELD_CONFIG_INDIVIDUAL_FOO_DESC="These settings apply for all foo." COM_FOOS_FIELD_CONFIG_INDIVIDUAL_FOO_DISPLAY="Foo"

COM_FOOS_FIELD_PUBLISH_DOWN_LABEL="Finish Publishing" COM_FOOS_FIELD_PUBLISH_UP_LABEL="Start Publishing" COM_FOOS_N_ITEMS_PUBLISHED="%d foos published." COM_FOOS_N_ITEMS_PUBLISHED_1="%d foo published." COM_FOOS_N_ITEMS_UNPUBLISHED="%d foos unpublished." COM_FOOS_N_ITEMS_UNPUBLISHED_1="%d foo unpublished."

COM_FOOS_FOOS_FIELD_PUBLISH_DOWN_LABEL="Finish Publishing" COM_FOOS_FOOS_FIELD_PUBLISH_UP_LABEL="Start Publishing" COM_FOOS_FOOS_N_ITEMS_PUBLISHED="%d foos published." COM_FOOS_FOOS_N_ITEMS_PUBLISHED_1="%d foo published." COM_FOOS_FOOS_N_ITEMS_UNPUBLISHED="%d foos unpublished." COM_FOOS_FOOS_N_ITEMS_UNPUBLISHED_1="%d foo unpublished."

=== Create administrator/components/com_foos/sql/updates/1.13.0.sql ===

The administrator/components/com_foos/sql/updates/1.13.0.sql file is ....

==== Completed administrator/components/com_foos/sql/updates/1.13.0.sql file ====

The code for the administrator/components/com_foos/sql/updates/1.13.0.sql file is as follows:

ALTER TABLE `#__foos_details` ADD COLUMN `published` tinyint(1) NOT NULL DEFAULT 0 AFTER `alias`;

ALTER TABLE #__foos_details ADD COLUMN publish_up datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER alias;

ALTER TABLE #__foos_details ADD COLUMN publish_down datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER alias;

ALTER TABLE #__foos_details ADD KEY idx_state (published);

=== Changing administrator/components/com_foos/tmpl/foo/edit.php ===

The administrator/components/com_foos/tmpl/foo/edit.php file is ....

==== Completed administrator/components/com_foos/tmpl/foo/edit.php file ====

The code for the administrator/components/com_foos/tmpl/foo/edit.php file is as follows:

defined('_JEXEC') or die;

use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Router\Route;

HTMLHelper::('behavior.formvalidator'); HTMLHelper::('script', 'com_foos/admin-foos-letter.js', array('version' => 'auto', 'relative' => true));

$app = Factory::getApplication(); $input = $app->input;

// In case of modal $isModal = $input->get('layout') == 'modal' ? true : false; $layout = $isModal ? 'modal' : 'edit'; $tmpl = $isModal || $input->get('tmpl', '', 'cmd') === 'component' ? '&tmpl=component' : ''; ?>

getForm()->renderField('name'); ?> getForm()->renderField('access'); ?> getForm()->renderField('published'); ?> getForm()->renderField('publish_up'); ?> getForm()->renderField('publish_down'); ?> getForm()->renderField('catid'); ?>

=== Changing administrator/components/com_foos/tmpl/foos/default.php ===

The administrator/components/com_foos/tmpl/foos/default.php file is ....

==== Completed administrator/components/com_foos/tmpl/foos/default.php file ====

The code for the administrator/components/com_foos/tmpl/foos/default.php file is as follows:

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Route;

$canChange = true; ?>

items)) : ?>
items); foreach ($this->items as $i => $item) : ?>
id); ?>
escape($item->name); ?>
'; ?> escape($item->name); ?>
escape($item->category_title); ?>
							</td>
							<td class="small d-none d-md-table-cell">
								<?php echo $item->access_level; ?>
							</td>
							<td class="text-center">
								<div class="btn-group">
									<?php echo HTMLHelper::_('jgrid.published', $item->published, $i, 'foos.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>
								</div>	
							</td>
							<td class="d-none d-md-table-cell">
								<?php echo $item->id; ?>
							</td>
						</tr>
						<?php endforeach; ?>
					</tbody>
				</table>

			<?php endif; ?>
			<input type="hidden" name="task" value="">
			<input type="hidden" name="boxchecked" value="0">
			<?php echo HTMLHelper::_('form.token'); ?>
		</div>
	</div>
</div>

== Test your component ==

Now you can zip all files and install them via Joomla Extension Manager.

You have to run a new installation or fix the database due to the changes in the database.

After that you can

== Example in Joomla! ==

If you edit an item, you can now also specify the period to which it should be published.

File:as_j4_t_13_1.png

In the overview you can see if an item is published or not. If you click the symbol the state is changed.

File:as_j4_t_13_1.png

In category manager, the items are now also displayed with the correct state.

File:as_j4_t_13_1.png

== Component Contents ==

At this point in the tutorial, your component should contain the following files: {| border=1 | 1 | administrator/components/com_foos/Controller/DisplayController.php | this is the administrator entry point to the Foo component

unchanged
7
administrator/components/com_foos/Controller/FooController.php
this is the entry point to the Foo component
unchanged
-
1
administrator/components/com_foos/Extension/FoosComponent.php
the interface BootableExtensionInterface where a component class can load its internal class loader or register HTML services see https://github.com/joomla/joomla-cms/pull/20217
unchanged
-
7
administrator/components/com_foos/Field/FooField.php
the interface BootableExtensionInterface where a component class can load its internal class loader or register HTML services see https://github.com/joomla/joomla-cms/pull/20217
unchanged
-
7
administrator/components/com_foos/Model/FooModel.php
this is the model of the Foo component item
unchanged
-
6
administrator/components/com_foos/Model/FoosModel.php
this is the model of the Foo component
unchanged
-
1
administrator/components/com_foos/Service/HTML/AdministratorService.php
the html service see https://github.com/joomla/joomla-cms/pull/20217
unchanged
-
7
administrator/components/com_foos/Table/FooTable.php
the database table
unchanged
-
1
administrator/components/com_foos/View/Foos/HtmlView.php
file representing the view in the back end
unchanged
-
1
administrator/components/com_foos/services/provider.php
the service provider interface see https://github.com/joomla/joomla-cms/pull/20217
unchanged
-
6
administrator/components/com_foos/sql/install.mysql.utf8.sql
During the install/uninstall/update phase of a component, you can execute SQL queries through the use of this SQL text file
unchanged
-
6
administrator/components/com_foos/sql/uninstall.mysql.utf8.sql
During the install/uninstall/update phase of a component, you can execute SQL queries through the use of this SQL text file
unchanged
-
7
administrator/components/com_foos/forms/foo.xml
the default form in the back end
unchanged
-
7
administrator/components/com_foos/tmpl/foo/edit.php
the default view in the back end
unchanged
-
7
administrator/components/com_foos/tmpl/foo/modal.php
the default view in the back end
unchanged
-
1
administrator/components/com_foos/tmpl/foos/default.php
the default view in the back end
unchanged
-
7
administrator/components/com_foos/tmpl/foos/modal.php
the default view in the back end
unchanged
-
1
administrator/components/com_foos/foos.xml
this is an XML (manifest) file that tells Joomla! how to install our component.
unchanged
-
1
administrator/components/com_foos/script.php
the installer script
unchanged
-
8
administrator/components/com_foos/language/en-GB/en-GB.com_foos.ini
the language file
unchanged
-
8
administrator/components/com_foos/language/en-GB/en-GB.com_foos.sys.ini
the language file
unchanged
-
9
administrator/components/com_foos/config.php
the configuration
unchanged
-
10
administrator/components/com_foos/access.xml
the ACL file for permissen handling
unchanged
-
11a
administrator/components/com_foos/Rule/LetterRule.php
the server side validation rule
unchanged
-
2
components/com_foos/Controller/DisplayController.php
this is the frond end entry point to the Foo component
unchanged
-
4
components/com_foos/Model/FooModel.php
this is the frond end model for the Foo component
unchanged
-
2
components/com_foos/View/Foo/HtmlView.php
file representing the view in the frond end
unchanged
-
7
components/com_foos/View/Foos/HtmlView.php
file representing the view in the frond end
unchanged
-
2
components/com_foos/tmpl/foo/default.php
the default view in the frond end
unchanged
-
3
components/com_foos/tmpl/foo/default.xml
the xml for the menu item
unchanged
-
8
components/com_foos/language/en-GB/en-GB.com_foos.ini
the language file
unchanged
-
8
components/com_foos/language/en-GB/en-GB.com_foos.sys.ini
the language file
unchanged
-
7
media/com_foos/js/admin-foos-modal.js
the javascript file
unchanged
-
11b
media/com_foos/js/admin-foos-letter.js
the client side validation rule
unchanged
-
}

== Conclusion ==

Now you can schedule the publication of your items. Now we continue with custom fields