4

I am making a dashboard widget to trigger the assets indexing action. I got the controller ready but I do not understand then how to trigger the action.

Should I go with AssetIndexTool? Then performAction()?
Or using ToolsController ?

I am really lost. Thanks for help!

5

I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going:

  • The classname of the tool i.e AssetIndex
  • An array of asset sources or * for them all
  • An indexing session id
  • An array of parameters to send to the tool

So I managed to get something working with the following code (this would go in your plugin controller)

public function actionUpdateIndex()
{
  // Get the AssetIndex tool
  $tool = craft()->components->getComponentByTypeAndClass(ComponentType::Tool, 'AssetIndex');

  // Generate an AssetIndex session id for us to use
  $sessionId = craft()->assetIndexing->getIndexingSessionId();

  // Set up some params
  $params = array(
    'start' => '1', // Tells the tool this is the initial request
    'sources' => '*', // All of them or [1,2,3] just ids1
  );

  // Get an initial response from the tool, will contain an array containing the assets to index
  $response = $tool->performAction($params);

  // See [1]
  foreach($response['batches'] as $key => $value)
  {
    foreach($value as $key => $batch)
    {
       // All the needed params are set in the array to do the index
       $tool->performAction($batch['params']);
    }
  }

}

Each time you call $tool->performAction($batch['params']); you will an array back, something like:

Array
(
 [success] => 1
)

[1] Because this is the initial request craft will return an array with a batches index, which we then have to do a few loops through, it looks something like this:

Array
(
 [batches] => Array
  (
   [0] => Array
    (
     [0] => Array
      (
       [params] => Array
        (
         [sessionId] => 6d08c5ba-169f-4b99-a7bd-cdc26c9428c7
         [sourceId] => 1
         [total] => 12
         [offset] => 0
         [process] => 1
        )

       )

      )

    )

 [total] => 0
)

Not sure if this is the absolute best way to do it, I was learning as I was going, but this worked for me so hopefully it'll work for you too :)

| improve this answer | |
  • Ouah thank you so much Alec. OK I tried but got a bug. Perhaps I am missing something here : foreach($response['batches'] as $key => $value). From where do we get the array $response['batches'] first time? – migswd Aug 16 '15 at 12:55
  • 1
    Oh dang, I missed out a piece of the puzzle, i've updated the answer, just below where the $params are set – Alec Ritson Aug 16 '15 at 12:57
  • Awesome :) You are the boss it works fantastic! Just one last detail: its adding the new files in the index but it is not deleting the removed files (related to the popup that appear to display missing files and ask confirmation to remove in the Craft Settings). There should be a param to provide to performAction no? ($params['deleteFile'] ??) – migswd Aug 16 '15 at 13:08
  • No worries! Yeah it looks that way, but an issue seems to be that the tool expects a template assets/_missing_items to be present and then from that somehow craft adds the deleteFile and deleteFolder indexes to the array. Sorry but I'm not too sure how to handle that at the moment... – Alec Ritson Aug 16 '15 at 13:40
  • OK thx that already great. I think I can be ok with files not deleted in my case. Thx for your time and help. – migswd Aug 16 '15 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.