Created
December 18, 2017 15:42
-
-
Save dannylamb/50c6753dc54cedfb7e8ab1c04f820d6e to your computer and use it in GitHub Desktop.
some queyr work to remember later
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
function islandora_node_insert(NodeInterface $node) { | |
_node_index($node); | |
} | |
function islandora_node_update(NodeInterface $node) { | |
dsm("IN NODE UPDATE"); | |
_node_index($node); | |
} | |
function _node_index(NodeInterface $node) { | |
$provider = new NodeContextProvider($node); | |
$provided = $provider->getRuntimeContexts([]); | |
$context_manager = \Drupal::service('context.manager'); | |
$context_manager->evaluateContexts($provided); | |
foreach ($context_manager->getActiveReactions('index') as $reaction) { | |
$reaction->execute($node); | |
} | |
} | |
function islandora_media_insert(MediaInterface $media) { | |
_media_index($media); | |
} | |
function islandora_media_update(MediaInterface $media) { | |
dsm("IN MEDIA UPDATE"); | |
_media_index($media); | |
} | |
function _media_index(MediaInterface $media) { | |
// Create a context for the media. | |
$provider = new MediaContextProvider($media); | |
$provided = $provider->getRuntimeContexts([]); | |
$nids = _get_referencing_nodes($media); | |
_execute_context_reactions('index', $nids, $media, $provided); | |
} | |
// I AM TRYING TO FIGURE OUT WHERE THE QUERY EXCEPTION IS COMING FROM | |
function _get_referencing_nodes(MediaInterface $media) { | |
// Get all node fields that are entity references to Media. | |
$fields = \Drupal::entityQuery('field_storage_config') | |
->condition('entity_type', 'node') | |
->condition('type', 'entity_reference') | |
->condition('settings.target_type', 'media') | |
->execute(); | |
// Process field names, stripping off 'node.' and appending 'target_id' | |
$conditions = array_map( | |
function($field) { return ltrim($field, 'node.') . '.target_id'; }, | |
$fields | |
); | |
if (empty($conditions)) { | |
return []; | |
} | |
// I'm RIGHT HERE | |
// Get all nodes that reference this media. | |
$query = \Drupal::entityQuery('node', 'OR'); | |
foreach ($conditions as $condition) { | |
$query->condition($condition, $media->id()); | |
} | |
return $query->execute(); | |
} | |
function _execute_context_reactions($reaction_type, $nids, $entity, array $provided) { | |
$context_manager = \Drupal::service('context.manager'); | |
// If there are no referencing nodes, just fire the reactions with the Media in context. | |
if (empty($nids)) { | |
$context_manager->evaluateContexts($provided); | |
foreach ($context_manager->getActiveReactions($reaction_type) as $reaction) { | |
$reaction->execute($entity); | |
} | |
} | |
// Otherwise, fire reactions per node / media combo. | |
else { | |
dsm($nids); | |
foreach ($nids as $nid) { | |
$node = \Drupal\node\Entity\Node::load($nid); | |
if (!$node) { | |
continue; | |
} | |
$provider = new NodeContextProvider($node); | |
$context_manager->evaluateContexts($provided + $provider->getRuntimeContexts([])); | |
foreach ($context_manager->getActiveReactions($reaction_type) as $reaction) { | |
$reaction->execute($entity); | |
} | |
} | |
} | |
} | |
function _file_index(FileInterface $file) { | |
$provider = new FileContextProvider($file); | |
$provided = $provider->getRuntimeContexts([]); | |
$references = file_get_file_references($file, NULL, \Drupal\Core\Entity\EntityStorageInterface::FIELD_LOAD_CURRENT, NULL); | |
// Ignore Drupal thumbnails. | |
unset($references['thumbnail']); | |
foreach ($references as $field => $reference) { | |
foreach ($reference['media'] as $media) { | |
$provider = new MediaContextProvider($media); | |
_execute_context_reactions( | |
'index', | |
_get_referencing_nodes($media), | |
$media, | |
$provider->getRuntimeContexts([]) + $provided | |
); | |
} | |
} | |
} | |
function islandora_file_insert(FileInterface $file) { | |
_file_index($file); | |
} | |
function islandora_file_update(FileInterface $file) { | |
_file_index($file); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment