简体   繁体   English

Joomla插件唯一别名

[英]Joomla plugin unique alias

I need to auto-generate different aliases from two articles with the same title in Joomla 3.3. 我需要从Joomla 3.3中标题相同的两篇文章中自动生成不同的别名。 The user will add articles in the front end. 用户将在前端添加文章。 I found this code: 我发现此代码:

<?php

defined( '_JEXEC' ) or die;

class plgContentRandom_Alias extends JPlugin
{

  function onContentBeforeSave($context, &$article, $isNew) {


  if(!$isNew){
    return;
  }

  $alias = $article->alias;
  $n = substr( "abcdefghijklmnopqrstuvwxyz" ,mt_rand( 0 ,25 ) ,1 ) .substr( md5( time( ) ) ,1 );


  $table = JTable::getInstance('content');
  while ($table->load(array('alias' => $alias))) {
    $new_alias = $alias . $n;
  }
  $article->alias = $new_alias;   
  return true;
  }

}
?>

, and made a plugin for Joomla, but the plugin not working in Joomla 3.3. ,并为Joomla制作了插件,但该插件在Joomla 3.3中不起作用。

Any suggestions? 有什么建议么?

You can write your own plugin using the following code, although this functionality should be already part of joomla core. 您可以使用以下代码编写自己的插件,尽管此功能应该已经是joomla核心的一部分。 I've used this because of Seblod error when using its insert content form. 我之所以使用它,是因为在使用其插入内容形式时出现Seblod错误。

Files: 文件:

Joomla installer descriptor: uniqueAliasGenerator.xml Joomla安装程序描述符: uniqueAliasGenerator.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="content" method="upgrade">
    <name>Content - Unique alias generator</name>
    <author>McGiogen</author>
    <creationDate>May 2015</creationDate>
    <copyright></copyright>
    <license></license>
    <authorEmail>mcgiogen@hotmail.it</authorEmail>
    <authorUrl>www.joomla.org</authorUrl>
    <version>1.0</version>
    <description>
        Automatic generator of unique alias.
        At save time it append "-X" (where X is a numeric identifier)
        if article alias is already in database.
    </description>
    <files>
        <filename plugin="uniqueAliasGenerator">uniqueAliasGenerator.php</filename>
        <filename>index.html</filename>
    </files>
    <config>
    </config>
</extension>

Plugin code: uniqueAliasGenerator.php 插件代码: uniqueAliasGenerator.php

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class plgContentUniqueAliasGenerator extends JPlugin
{   
    /**
     * Alias check and generation before save content method.
     * Content is passed by reference. Method is called before the content is saved.
     *
     * @param   string  $context  The context of the content passed to the plugin (added in 1.6).
     * @param   object  $article  A JTableContent object.
     * @param   bool    $isNew    If the content is just about to be created.
     *
     * @return  void
     */
    public function onContentBeforeSave($context, $article, $isNew)
    {
        if ($context == 'com_content.article' && $article->alias && $isNew) {
            $oldAlias = $article->alias;
            $categoryId = $article->catid;  //An alias must be unique only in its category
            $article->alias = $this->getUniqueAlias($oldAlias, $categoryId);
        }
        return true;
    }

    /**
     * Find unique Alias name if current doesn't exist.
     * @param string $alias Alias of the article
     * @param string $catId Id of article's category
     * 
     * @return string Return the unique alias value.
     */
    protected function getUniqueAlias($alias, $catId)
    {
        $alias_ini = $alias;

        for ($i = 2; $this->isAliasExist($alias, $catId); $i++) {
            $alias = $alias_ini . '-' . $i;   
        }
        return $alias;
    }

    /**
     * Check the 'alias' in the database.
     *
     * @return boolean If found return true else false.
     */
    protected function isAliasExist($alias, $catId) 
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query
            ->select('COUNT(*)')
            ->from($db->quoteName('#__content'))    //Articles table
            ->where($db->quoteName('alias') . ' = ' . $db->quote($alias))
            ->where($db->quoteName('catid') . ' = ' . $db->quote($catId));  //Category ID
        $db->setQuery($query);

        return ($db->loadResult() ? true : false);
    }
}
?>

index.html 的index.html

<!DOCTYPE html><title></title>

How to use: 如何使用:

Create files with the same names, put them in a folder called "uniqueAliasGenerator", zip in "uniqueAliasGenerator.zip", upload and install on your joomla. 创建具有相同名称的文件,将其放在名为“ uniqueAliasGenerator”的文件夹中,在“ uniqueAliasGenerator.zip”中压缩,然后上传并安装到您的joomla上。

Compatible with Joomla 3.x, tested on Joomla 3.4.1 与Joomla 3.x兼容,已在Joomla 3.4.1上进行了测试

Update 11 Nov 2017 更新2017年11月11日

Added check of $isNew. 添加了对$ isNew的检查。 Thanks @robert-drygas. 谢谢@ robert-drygas。

In McGiogen code the line 在McGiogen代码行中

if ($context == 'com_content.article' && $article->alias) {

can be write as 可以写成

if ($context == 'com_content.article' && $article->alias && $isNew) {

so unique alias will be ganerated only for the new articles (without altered existing aliases when editing old articles). 因此,唯一的别名将仅针对新文章进行合并(在编辑旧文章时不会更改现有别名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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