Skip to content

Kévin Dunglas

Founder of Les-Tilleuls.coop (worker-owned cooperative). Creator of API Platform, FrankenPHP, Mercure.rocks, Vulcain.rocks and of some Symfony components.

Menu
  • Talks
  • Resume
  • Sponsor me
  • Contact
Menu

Tag Suggestion for symfony

Posted on August 25, 2009August 31, 2009 by Kévin Dunglas

I just have created a patch for sfFormExtraPlugin. It provides a new form widget called sfWidgetFormJqueryTagSuggestion designed to suggest tags. It is based on the Tag Suggestion jQuery plugin of Remy Sharp.

Capture d'écran

You can download it from GitHub, just copy the files to the corresponding directories of your existing sfFormExtraPlugin installation or wait for upstream inclusion of my patch.

This widget integrates easily with sfPropelActAsTaggableBehaviorPlugin. Here is how to use it with this plugin.

You must have sfFormExtraPlugin with my patch and sfPropelActAsTaggableBehaviorPlugin properly installed and working. We consider that you have a model called Article with the taggable behavior.

Add jQuery to your javascripts

Like many others sfFormExtraPlugin widgets, the sfWidgetFormJqueryTagSuggestion requires jQuery to work. If you already have it installed, you can skip this step. Either, download jQuery to the web/js/ directory of your project, open the config/view.yml file of your application and set it like that:

default:
  javascripts:    [jquery-1.3.2.min.js]

Of course, change the filename if needed.

Put Javascripts and CSS into your web directory

sfFormExtraPlugin need some CSS and Javascripts to work properly, run the symfony plugin:publish-assets command to put them into corrects directories.

Create a static function to retrieve matching tags

Its time to extend the TagPeer model class to add it a method allowing to retrieve tags corresponding to a query. Creates a new file called MyTagPeer.class.php into the lib/model/ directory of your project containing the following code:

class MyTagPeer extends TagPeer {
  /**
   * Gets tags for a selector
   *
   * @param string $q
   * @param int $limit
   * @return array
   */
  static public function retrieveForSelect($q, $limit = 10)
  {
    $criteria = new Criteria();
    $criteria->add(TagPeer::NAME, '%'.$q.'%', Criteria::LIKE);
    $criteria->addAscendingOrderByColumn(TagPeer::NAME);
    $criteria->setLimit($limit);

      $tags = array();
      foreach (TagPeer::doSelect($criteria) as $tag) {
        $tags[] = $tag->getName();
      } 

    return $tags;
  }
}

Do not forget to run symfony cc to clear the cache after that.

Create a controller method to send the result as JSON

We have the model, we need the controller. Create a new module called tag by running symfony generate:module yourapp tag and add the following method to the tagActions class:

/**
   * Displays a JSON encoded tags array
   *
   * @param sfWebRequest $request
   * @return string
   */
  public function executeSelect(sfWebRequest $request)
  {
    $this->getResponse()->setContentType('application/json');
    $tags = MyTagPeer::retrieveForSelect($request->getParameter('tag'));

    return $this->renderText(json_encode($tags));
  }

Open your routing.yml file and add the following line to be able to access this action:

tag_select:
  url:          /select-tag
  param:     { module: tag, action: select }

Modify your form to add the new tags suggestion widget

Open the form class you want to add this widget and add the following line to the end of the configure method:

$this->widgetSchema['tags'] = new sfWidgetFormJqueryTagSuggestion(array('url' => $this->getOption('url'), 'separator' => ', '));

We use the comma as separator instead of the space by default because this is the separator used by sfPropelActAsTaggableBehaviorPlugin.

Next, modify your call to the form in your actions to pass the AJAX URL as form option:

$this->form = new ArticleForm($article,
  array('url' => $this->generateUrl('article_select_tag'))
);

It’s done!

Related posts:

  1. DunglasTodoMVCBundle compatible with Symfony 2.4
  2. Making the Symfony PropertyAccess Component 84% faster in 2.8… and counting
  3. Un formulaire de création de compte avec Symfony et sfGuard
  4. Generate a Symfony password hash from the command line

4 thoughts on “Tag Suggestion for symfony”

  1. xavier says:
    August 31, 2009 at 6:05 am

    Hi Kévin,

    Nice idea indeed !

    Why wouldn't you propose a patch on Ymfony's trac in order to ask for the integration of your patch in sfFormExtraPlugin ? Or in sfPropelAstAsTaggableBehaviorPlugin ?

    cheers,

    Xavier

    Reply
  2. Kévin Dunglas says:
    August 31, 2009 at 7:26 am

    Hello,
    I've already submitted a patch in trac for sfFormExtraPlugin (there is the link into the article): http://trac.symfony-project.org/ticket/7073

    I'll make a patch for sfPropelAstAsTaggableBehaviorPlugin.

    Reply
  3. xavier says:
    August 31, 2009 at 9:33 am

    Cool,

    Then I'll integrate it in the plugin so soon 🙂

    xavier

    Reply
  4. Buy Hello Kitty says:
    March 30, 2011 at 8:17 pm

    Hey very cool site!! Man .. Excellent .. Amazing .. I’ll bookmark your web site and take the feeds also…I am happy to find numerous useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

    Reply

Leave a ReplyCancel reply

Social

  • Bluesky
  • GitHub
  • LinkedIn
  • Mastodon
  • X
  • YouTube

Links

  • API Platform
  • FrankenPHP
  • Les-Tilleuls.coop
  • Mercure.rocks
  • Vulcain.rocks

Subscribe to this blog

Top Posts & Pages

  • JSON Columns and Doctrine DBAL 3 Upgrade
  • Securely Access Private Git Repositories and Composer Packages in Docker Builds
  • Preventing CORS Preflight Requests Using Content Negotiation
  • Symfony's New Native Docker Support (Symfony World)
  • FrankenPHP: The Modern Php App Server, written in Go
  • Develop Faster With FrankenPHP
  • PHP and Symfony Apps As Standalone Binaries
  • How to debug Xdebug... or any other weird bug in PHP
  • HTTP compression in PHP (new Symfony AssetMapper feature)
  • Generate a Symfony password hash from the command line

Tags

Apache API API Platform Buzz Caddy Docker Doctrine FrankenPHP Go Google GraphQL HTTP/2 Hydra hypermedia Hébergement Javascript JSON-LD Kubernetes La Coopérative des Tilleuls Les-Tilleuls.coop Lille Linux Mac Mercure Mercure.rocks Messagerie Instantanée MySQL performance PHP Punk Rock Python React REST Rock'n'Roll Schema.org Security SEO SEO Symfony Symfony Live Sécurité Ubuntu Web 2.0 webperf XML

Archives

Categories

  • DevOps (84)
    • Ubuntu (68)
  • Go (17)
  • JavaScript (46)
  • Mercure (7)
  • Opinions (91)
  • PHP (170)
    • API Platform (77)
    • FrankenPHP (9)
    • Laravel (1)
    • Symfony (97)
    • Wordpress (6)
  • Python (14)
  • Security (15)
  • SEO (25)
  • Talks (46)
© 2025 Kévin Dunglas | Powered by Minimalist Blog WordPress Theme