Conditions | 16 |
Paths | 418 |
Total Lines | 79 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
87 | function magnific_popup_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) { |
||
88 | // Items are some kind of entity reference, be it via image or file type. |
||
89 | // Load the corresponding items into a common format for our formatter_view(). |
||
90 | $static = &drupal_static(__FUNCTION__, ['loaded_items' => [], 'thumbnail_schemes' => NULL]); |
||
91 | $loaded_items = &$static['loaded_items']; |
||
92 | $thumbnail_schemes = &$static['thumbnail_schemes']; |
||
93 | if (!isset($thumbnail_schemes)) { |
||
94 | $thumbnail_schemes = module_invoke_all('magnific_popup_thumbnail_schemes'); |
||
95 | } |
||
96 | // Iterate through $items looking for valid gallery items. Refactor them into |
||
97 | // a consistent format for magnific_popup_field_formatter_view(). |
||
98 | $wrappers = []; |
||
99 | foreach ($entities as $entity_id => $entity) { |
||
100 | foreach ($items[$entity_id] as $delta => &$item) { |
||
101 | // Fail-safe check to not load items |
||
102 | if (empty($item['uri'])) { |
||
103 | continue; |
||
104 | } |
||
105 | // Check if we've already parsed this fid, and build it if not. |
||
106 | if (!isset($loaded_items[$item['fid']])) { |
||
107 | $loaded_items[$item['fid']] = FALSE; |
||
108 | $scheme = file_uri_scheme($item['uri']); |
||
109 | // Attempt to re-use, or load from scratch, a wrapper for this scheme. |
||
110 | if (empty($wrappers[$scheme])) { |
||
111 | // Create a new wrapper. |
||
112 | $wrappers[$scheme] = file_stream_wrapper_get_instance_by_uri($item['uri']); |
||
113 | } |
||
114 | else { |
||
115 | // Attempt to re-use an existing wrapper. |
||
116 | $wrappers[$scheme]->setUri($item['uri']); |
||
117 | } |
||
118 | // Check that the resource was accessible. |
||
119 | if ($wrappers[$scheme] && FALSE !== $wrappers[$scheme]->url_stat($item['uri'], STREAM_URL_STAT_QUIET)) { |
||
120 | $wrapper = $wrappers[$scheme]; |
||
121 | // Determine the file's info and store it. |
||
122 | $thumbnail_path = FALSE; |
||
123 | // MediaYouTubeStreamWrapper implements getLocalThumbnailPath() to |
||
124 | // find the thumbnail path. |
||
125 | if (method_exists($wrapper,'getLocalThumbnailPath')) { |
||
126 | $thumbnail_path = $wrapper->getLocalThumbnailPath(); |
||
127 | } |
||
128 | // Try the standard method to get a thumbnail. |
||
129 | else { |
||
130 | if ($uri = $wrapper->getUri()) { |
||
131 | $thumbnail_path = $uri; |
||
132 | } |
||
133 | } |
||
134 | // Adjust $thumbnail_path based on the $scheme. |
||
135 | // @todo Make the default thumbnail_style configurable. |
||
136 | $thumbnail_style = 'magnific_popup_thumbnail'; |
||
137 | if (!empty($thumbnail_schemes[$scheme])) { |
||
138 | $thumbnail_style = $thumbnail_schemes[$scheme]; |
||
139 | } |
||
140 | $thumbnail_path = image_style_url($thumbnail_style, $thumbnail_path); |
||
141 | // Get the title if it exists, or fallback to the filename. |
||
142 | $title = !empty($item['title']) ? $item['title'] : $item['filename']; |
||
143 | // Get the alt if it exists, or fallback to the title. |
||
144 | $alt = !empty($item['alt']) ? $item['alt'] : $title; |
||
145 | // Build the finished gallery item. |
||
146 | $gallery_item = [ |
||
147 | 'item' => $item, |
||
148 | 'title' => $title, |
||
149 | 'alt' => $alt, |
||
150 | 'thumbnail_path' => $thumbnail_path, |
||
151 | 'target_uri' => $wrapper->getExternalUrl() |
||
152 | ]; |
||
153 | $loaded_items[$item['fid']] = $gallery_item; |
||
154 | } |
||
155 | } |
||
156 | // Replace $item with the parsed version of info for this fid. |
||
157 | $item = $loaded_items[$item['fid']]; |
||
158 | // Check if this item was unloadable and if so remove it from the list. |
||
159 | if (empty($item)) { |
||
160 | unset($items[$entity_id][$delta]); |
||
161 | } |
||
162 | } |
||
163 | // Remove the pointer. |
||
164 | if (isset($item)) { |
||
165 | unset($item); |
||
166 | } |
||
278 |