Skip to main content
Missed out a piece of the puzzle
Source Link
Alec Ritson
  • 4.5k
  • 1
  • 20
  • 34
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']);
    }
  }
  
}
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
  );

  // 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']);
    }
  }
  
}
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']);
    }
  }
  
}
Source Link
Alec Ritson
  • 4.5k
  • 1
  • 20
  • 34

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
  );

  // 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 :)