Elgg  Version master
CreateDefaultWidgetsHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Widgets;
4 
11 
21  public function __invoke(\Elgg\Event $event) {
22  static $processed_events = [];
23 
24  if (isset($processed_events["{$event->getName()}.{$event->getType()}"])) {
25  return;
26  }
27 
28  // only create default widgets once per event
29  $processed_events["{$event->getName()}.{$event->getType()}"] = true;
30 
31  $entity = $event->getObject();
32  if (!$entity instanceof \ElggEntity) {
33  return;
34  }
35 
36  $default_widget_info = elgg_trigger_event_results('get_list', 'default_widgets', [], []);
37  if (empty($default_widget_info)) {
38  return;
39  }
40 
41  $type = $entity->getType();
42  $subtype = $entity->getSubtype();
43 
44  foreach ($default_widget_info as $info) {
45  if (elgg_extract('event_name', $info) !== $event->getName()) {
46  continue;
47  }
48 
49  if (elgg_extract('event_type', $info) !== $event->getType()) {
50  continue;
51  }
52 
53  if (elgg_extract('entity_type', $info) !== $type) {
54  continue;
55  }
56 
57  $entity_subtype = elgg_extract('entity_subtype', $info, ELGG_ENTITIES_ANY_VALUE);
58  if ($entity_subtype !== ELGG_ENTITIES_ANY_VALUE && $entity_subtype !== $subtype) {
59  continue;
60  }
61 
62  $widget_context = elgg_extract('widget_context', $info);
63  if (empty($widget_context)) {
64  continue;
65  }
66 
67  // need to be able to access everything
68  elgg_push_context('create_default_widgets');
69 
71  // check if there are already widgets
73  'type' => 'object',
74  'subtype' => 'widget',
75  'owner_guid' => $entity->guid,
76  'metadata_name' => 'context',
77  'metadata_value' => $widget_context,
78  ])) {
79  return;
80  }
81 
82  // pull in by widget context with widget owners as the site
83  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
85  'type' => 'object',
86  'subtype' => 'widget',
87  'owner_guid' => elgg_get_site_entity()->guid,
88  'metadata_name' => 'context',
89  'metadata_value' => $widget_context,
90  'limit' => false,
91  'batch' => true,
92  ]);
93 
94  /* @var \ElggWidget[] $widgets */
95  foreach ($widgets as $widget) {
96  // change the container and owner
97  $new_widget = clone $widget;
98  $new_widget->container_guid = $entity->guid;
99  $new_widget->owner_guid = $entity->guid;
100 
101  $new_widget->save();
102  }
103  });
104 
106  }
107  }
108 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:304
if(empty($page_owner)||$owner->guid!==$page_owner->guid) $widgets
Definition: widgets.php:40
$type
Definition: delete.php:21
elgg_trigger_event_results(string $event, string $type, array $params=[], $returnvalue=null)
Triggers an event where it is expected that the mixed return value could be manipulated by event call...
Definition: events.php:117
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: context.php:62
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
const ELGG_ENTITIES_ANY_VALUE
Constant to request the value of a parameter be ignored in elgg_get_*() functions.
Definition: constants.php:21
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
if($guid===false) $widget
Definition: add.php:31
elgg_push_context(string $context)
Push a context onto the top of the stack.
Definition: context.php:52
$subtype
Definition: delete.php:22
__invoke(\Elgg\Event $event)
This event handler is registered for events based on what kinds of default widgets have been register...
Models an event passed to event handlers.
Definition: Event.php:11
$widget_context