You are here

function filehash_node_build_defaults_alter in File Hash 8

Implements hook_ENTITY_TYPE_build_defaults_alter().

File

./filehash.module, line 168
Generate hashes for each uploaded file.

Code

function filehash_node_build_defaults_alter(array &$build, EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
  if ($view_mode != 'rss') {
    return;
  }

  // The <media:hash> element only supports MD5 and SHA-1.
  $algos = filehash_algos();
  if (!isset($algos['md5']) && !isset($algos['sha1'])) {
    return;
  }

  // The following field types are currently supported.
  $fields = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'entity_type' => 'node',
    'bundle' => $node
      ->bundle(),
    'field_type' => 'file',
  ]);
  $fields += \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'entity_type' => 'node',
    'bundle' => $node
      ->bundle(),
    'field_type' => 'image',
  ]);
  foreach ($fields as $field) {
    if (method_exists($field, 'getName')) {
      foreach ($node->{$field
        ->getName()} as $item) {
        if ($item
          ->isDisplayed()) {

          // Add <media:hash> elements for at most one file per RSS item.
          $file = File::load($item->target_id);
          filehash_rss_elements($file, $node);
          return;
        }
      }
    }
  }
}