caption-drush-script.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. //
  3. // This example demonstrates how to write a drush
  4. // script. These scripts are run with the php:script command.
  5. //
  6. use Drush\Drush;
  7. // $this->output()->writeln("Hello world!");
  8. // $this->output()->writeln("The extra options/arguments to this command were:");
  9. // $this->output()->writeln(print_r($extra, true));
  10. //
  11. // We can check which site was bootstrapped via
  12. // the '@self' alias, which is defined only if
  13. // there is a bootstrapped site.
  14. //
  15. $self = Drush::aliasManager()->getSelf();;
  16. if (!$self->hasRoot()) {
  17. $this->output()->writeln('No bootstrapped site.');
  18. }
  19. else {
  20. $this->output()->writeln('The following site is bootstrapped:');
  21. $this->output()->writeln(print_r($self->legacyRecord(), true));
  22. // $nids = \Drupal::entityQuery('node')->condition('type','projet')->execute();
  23. $nodes = \Drupal::entityTypeManager()->getStorage('node')
  24. ->loadByProperties(['type' => 'projet']);
  25. foreach ($nodes as $nid => $node) {
  26. /** @var Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field_photo */
  27. $field_photo = $node->get('field_photo');
  28. $itemlist = $field_photo->getValue();
  29. /** @var Drupal\image_field_caption\ImageCaptionItem $field_item */
  30. foreach ($itemlist as $key => $field_item) {
  31. if (!empty($field_item['alt']) && (!isset($field_item['caption']) || empty($field_item['caption']))) {
  32. $field_item['image_field_caption']['value'] = $field_item['alt'];
  33. $field_item['image_field_caption']['format'] = 'plain_text';
  34. $itemlist[$key] = $field_item;
  35. }
  36. }
  37. // $field_photo->setValue($itemlist);
  38. $node->set('field_photo', $itemlist);
  39. $node->save();
  40. }
  41. }