Elgg  Version 4.3
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  // only create default widgets once per event
28  $processed_events["{$event->getName()}.{$event->getType()}"] = true;
29 
30  $entity = $event->getObject();
31  if (!$entity instanceof \ElggEntity) {
32  return;
33  }
34 
35  $default_widget_info = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, []);
36  if (empty($default_widget_info)) {
37  return;
38  }
39 
40  $type = $entity->getType();
41  $subtype = $entity->getSubtype();
42 
43  foreach ($default_widget_info as $info) {
44  if (elgg_extract('event_name', $info) !== $event->getName()) {
45  continue;
46  }
47 
48  if (elgg_extract('event_type', $info) !== $event->getType()) {
49  continue;
50  }
51 
52  if (elgg_extract('entity_type', $info) !== $type) {
53  continue;
54  }
55 
56  $entity_subtype = elgg_extract('entity_subtype', $info, ELGG_ENTITIES_ANY_VALUE);
57  if ($entity_subtype !== ELGG_ENTITIES_ANY_VALUE && $entity_subtype !== $subtype) {
58  continue;
59  }
60 
61  $widget_context = elgg_extract('widget_context', $info);
62  if (empty($widget_context)) {
63  continue;
64  }
65 
66  // need to be able to access everything
67  elgg_push_context('create_default_widgets');
68 
70  // check if there are already widgets
72  'type' => 'object',
73  'subtype' => 'widget',
74  'owner_guid' => $entity->guid,
75  'private_setting_name' => 'context',
76  'private_setting_value' => $widget_context,
77  ])) {
78  return;
79  }
80 
81  // pull in by widget context with widget owners as the site
82  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
84  'type' => 'object',
85  'subtype' => 'widget',
86  'owner_guid' => elgg_get_site_entity()->guid,
87  'private_setting_name' => 'context',
88  'private_setting_value' => $widget_context,
89  'limit' => false,
90  'batch' => true,
91  ]);
92 
93  /* @var \ElggWidget[] $widgets */
94  foreach ($widgets as $widget) {
95  // change the container and owner
96  $new_widget = clone $widget;
97  $new_widget->container_guid = $entity->guid;
98  $new_widget->owner_guid = $entity->guid;
99 
100  // pull in settings
101  $settings = $widget->getAllPrivateSettings();
102 
103  foreach ($settings as $name => $value) {
104  $new_widget->$name = $value;
105  }
106 
107  $new_widget->save();
108  }
109  });
110 
112  }
113  }
114 }
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:592
elgg_push_context($context)
Push a context onto the top of the stack.
Definition: context.php:52
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$type
Definition: delete.php:21
if($type!= 'user') $settings
Definition: save.php:16
$value
Definition: generic.php:51
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:146
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:556
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:380
Models an event passed to event handlers.
Definition: Event.php:11
const ELGG_ENTITIES_ANY_VALUE
Definition: constants.php:24
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
if($guid===false) $widget
Definition: add.php:30
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$widgets
Definition: widgets.php:37
$subtype
Definition: delete.php:22
__invoke(\Elgg\Event $event)
This plugin hook handler is registered for events based on what kinds of default widgets have been re...
$widget_context