mmikkel /
CpFieldInspect-Craft
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * CP Field Inspect plugin for Craft CMS 5.x |
||
| 4 | * |
||
| 5 | * Inspect field handles and easily edit field settings |
||
| 6 | * |
||
| 7 | * @link http://mmikkel.no |
||
| 8 | * @copyright Copyright (c) 2017 Mats Mikkel Rummelhoff |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace mmikkel\cpfieldinspect; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\base\Element; |
||
| 15 | use craft\base\Plugin; |
||
| 16 | use craft\elements\User; |
||
| 17 | use craft\events\DefineElementHtmlEvent; |
||
| 18 | use craft\events\DefineHtmlEvent; |
||
| 19 | use craft\events\PluginEvent; |
||
| 20 | use craft\events\TemplateEvent; |
||
| 21 | use craft\helpers\Cp; |
||
|
0 ignored issues
–
show
|
|||
| 22 | use craft\helpers\Html; |
||
| 23 | use craft\services\Plugins; |
||
| 24 | use craft\web\View; |
||
| 25 | |||
| 26 | use mmikkel\cpfieldinspect\helpers\CpFieldInspectHelper; |
||
| 27 | use mmikkel\cpfieldinspect\web\CpFieldInspectBundle; |
||
| 28 | |||
| 29 | use yii\base\Event; |
||
| 30 | use yii\web\View as ViewAlias; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Class CpFieldInspect |
||
| 35 | * @author Mats Mikkel Rummelhoff |
||
| 36 | * @package mmikkel\cpfieldinspect |
||
| 37 | * @since 1.0.0 |
||
| 38 | * |
||
| 39 | * Plugin icon credit: CUSTOMIZE SEARCH by creative outlet from the Noun Project |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | class CpFieldInspect extends Plugin |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function init(): void |
||
| 49 | { |
||
| 50 | |||
| 51 | parent::init(); |
||
| 52 | |||
| 53 | // If admin changes are disallowed, we have no purpose. |
||
| 54 | $allowAdminChanges = Craft::$app->getConfig()->getGeneral()->allowAdminChanges; |
||
| 55 | if (!$allowAdminChanges) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | // After installation, set the "Show field handles" user preference for active admin users |
||
| 60 | Event::on( |
||
| 61 | Plugins::class, |
||
| 62 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 63 | function (PluginEvent $event) { |
||
| 64 | if ($event->plugin !== $this) { |
||
|
0 ignored issues
–
show
|
|||
| 65 | return; |
||
| 66 | } |
||
| 67 | $adminUsers = User::find()->admin()->status(User::STATUS_ACTIVE)->all(); |
||
| 68 | foreach ($adminUsers as $adminUser) { |
||
| 69 | try { |
||
| 70 | Craft::$app->getUsers()->saveUserPreferences($adminUser, [ |
||
| 71 | 'showFieldHandles' => true, |
||
| 72 | ]); |
||
| 73 | } catch (\Throwable $e) { |
||
| 74 | Craft::error($e, __METHOD__); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | ); |
||
| 79 | |||
| 80 | // Eject for anything that isn't a CP request |
||
| 81 | $request = Craft::$app->getRequest(); |
||
| 82 | if (!$request->getIsCpRequest() || $request->getIsLoginRequest()) { |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Defer further initialisation to after app init |
||
| 87 | Craft::$app->onInit(function () { |
||
| 88 | try { |
||
| 89 | $this->doIt(); |
||
| 90 | } catch (\Throwable $e) { |
||
| 91 | Craft::error($e, __METHOD__); |
||
| 92 | } |
||
| 93 | }); |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return void |
||
| 99 | * @throws \Throwable |
||
| 100 | */ |
||
| 101 | protected function doIt(): void |
||
| 102 | { |
||
| 103 | |||
| 104 | // Do nothing if the user is not an admin, or if they don't have field handles visible |
||
| 105 | /** @var User $user */ |
||
| 106 | $user = Craft::$app->getUser()->getIdentity(); |
||
| 107 | if (!$user?->admin) { |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Make sure element cards and chips have a [data-type-id] attribute, which we'll use to bootstrap an "Edit Entry Type" link in their settings menus |
||
| 112 | foreach ([ |
||
| 113 | Cp::EVENT_DEFINE_ELEMENT_CARD_HTML, |
||
| 114 | Cp::EVENT_DEFINE_ELEMENT_CHIP_HTML, |
||
| 115 | ] as $eventName) { |
||
| 116 | Event::on( |
||
| 117 | Cp::class, |
||
| 118 | $eventName, |
||
| 119 | static function (DefineElementHtmlEvent $event) { |
||
| 120 | $typeId = $event->element?->typeId ?? null; |
||
| 121 | if (empty($typeId)) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | try { |
||
| 125 | $html = Html::modifyTagAttributes($event->html, [ |
||
| 126 | 'data-type-id' => $typeId, |
||
| 127 | ]); |
||
| 128 | } catch (\Throwable $e) { |
||
| 129 | Craft::error($e, __METHOD__); |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | $event->html = $html; |
||
| 133 | } |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Inject edit source buttons for elements that support the EVENT_DEFINE_META_FIELDS_HTML event |
||
| 138 | Event::on( |
||
| 139 | Element::class, |
||
| 140 | Element::EVENT_DEFINE_META_FIELDS_HTML, |
||
| 141 | static function (DefineHtmlEvent $event) { |
||
| 142 | if ($event->static) { |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | $event->html .= CpFieldInspectHelper::getEditElementSourceButton($event->sender); |
||
| 146 | } |
||
| 147 | ); |
||
| 148 | |||
| 149 | // Inject edit source buttons for elements that still use the old template hooks |
||
| 150 | Craft::$app->getView()->hook('cp.globals.edit.content', [CpFieldInspectHelper::class, 'renderEditSourceLink']); |
||
| 151 | Craft::$app->getView()->hook('cp.commerce.product.edit.details', [CpFieldInspectHelper::class, 'renderEditSourceLink']); |
||
| 152 | |||
| 153 | // Register asset bundle |
||
| 154 | Event::on( |
||
| 155 | View::class, |
||
| 156 | View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
||
| 157 | static function (TemplateEvent $event) { |
||
| 158 | if ($event->templateMode !== View::TEMPLATE_MODE_CP) { |
||
| 159 | return; |
||
| 160 | } |
||
| 161 | Craft::$app->getView()->registerAssetBundle(CpFieldInspectBundle::class, ViewAlias::POS_END); |
||
| 162 | } |
||
| 163 | ); |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | } |
||
| 168 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths